### Author Topic: Help with algorithm!  (Read 9486 times)

#### Freddy

• Guest
##### Help with algorithm!
« on: February 17, 2006, 01:13:13 am »
I'm building a program that can convert text string to ASCII sequences. It's some kind of message encoder. Soon I'll add encryption also.
I can do the conversion as follows:

"This is a test!"

to

"84 104 105 115 32 105 115 32 97 32 116 101 115 116 33"

without quotes.

But now I need to take an ASCII sequence and convert back to text string.
I'm thinking hard about some logic to do it, but can't get to work.
I need to convert something like:

"66 97 99 107 32 116 111 32 116 101 120 116 33"

to

"Back to text!"

Thanks!

#### Robert

• Member
• Posts: 196
##### ASCII sequence convert back to text string
« Reply #1 on: February 17, 2006, 06:33:09 am »
The BCX BASIC to C translator

http://www.bcxgurus.com/

does this using the BCX vchr function.

The BASIC code

DIM Text\$
Text\$ = vchr\$(66, 97, 99, 107, 32 116 111, 32, 116 101, 120, 116, 33)
PRINT Text\$

is converted by BCX to the C code

// *************************************************************
//   Created with BCX -- The BASIC To C Translator (ver pc5.08.1103b)
//  BCX (c) 1999, 2000, 2001, 2002, 2003, 2004, 2005 by Kevin Diggins
// *************************************************************
//     Translated for compiling with a C Compiler
// *************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

// *************************************************
//            User Global Variables
// *************************************************

static char    Text[2048];

// *************************************************
//               Standard Prototypes
// *************************************************

char*   BCX_TmpStr(size_t);
char*   vchr (int,...);

// *************************************************
//                  Main Program
// *************************************************

int main(int argc, char *argv[])
{
strcpy(Text,vchr(13,66,97,99,107,32,116,111,32,116,101,120,116,33));
printf("%s\n",Text);
return 0;   //  End of main program
}

// *************************************************
//                 Runtime Functions
// *************************************************

char *BCX_TmpStr (size_t Bites)
{
static int   StrCnt;
static char *StrFunc[2048];
StrCnt=(StrCnt + 1) & 2047;
if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
}

char *vchr(int charcount, ...)
{
register int c = 0, i = charcount;
char *s_ = BCX_TmpStr(charcount + 1);
va_list marker;
s_ = 0;
va_start(marker, charcount);
while(i-- > 0) s_[c++] = va_arg(marker,int);
va_end(marker);
return s_;
}

The first parameter in the vchr function is the count of the characters to be converted. This makes it possible to embed an ASCII 0 into the string.

Robert Wishlaw

#### kobold

• Guest
##### Help with algorithm!
« Reply #2 on: March 14, 2006, 08:25:58 pm »
Code: [Select]
`#include <string.h>#include <stdio.h>int main( void ){    int data[] = { 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 33 };    char tmp[64] = "";    for ( int i=0; i < sizeof(data) / sizeof(int); i++ )        tmp[i] = data[i];    printf( "Text: %s\n", tmp );    return(0);}`

#### Anonymous

• Guest
##### Help with algorithm!
« Reply #3 on: September 04, 2006, 12:09:19 pm »
Where you get it???

#### hellork

• Guest
##### Re: Help with algorithm!
« Reply #4 on: November 20, 2010, 11:32:42 pm »
This is not exactly re-entrant, so don't use it on a web server and stuff...

This is the executable source code. Run with Anchor (free) http://freshmeat.net/projects/anchor
Anchor can also convert this to C. It is C, actually. But Anchor corrects the punctuation errors for me

Code: [Select]
`#!/usr/local/bin/anch -run -Wall -g#include <stdio.h>#include <string.h>#define MAX 100char *input  char *msg    static char s[MAX+1]    printf  "%s",msg    while  fgets(s,MAX,stdin) && !strchr(s,'\n')    return schar *numText  char *msg    int t    static char s[2],ret[MAX+1]    for  msg = strtok(msg," ");msg;msg = strtok(NULL," ")        t = atoi  msg        if  t < '0' || t > 'z'            t = ' '        sprintf  s,"%c",t        strcat  ret,s    return retint main  void    char *s,*t    int i    s = input  "Please enter a string:\n"    t = numText  s    puts  t    return 0`

#### Vortex

• Member
• Posts: 529
##### Re: Help with algorithm!
« Reply #5 on: November 21, 2010, 01:09:43 am »
Hi hellork,

I guess you should create another topic to present the tool to the forum members.
Code it... That's all...

#### hellork

• Guest
##### Re: Help with algorithm!
« Reply #6 on: November 21, 2010, 01:54:27 am »
OK. I'll do that. I'm not sure how well it works on Windows command line yet, but maybe some people will want to test it.

#### TimoVJL

• Global Moderator
• Member
• Posts: 1899
##### Re: Help with algorithm!
« Reply #7 on: November 21, 2010, 03:56:23 pm »
An another way:
Code: [Select]
`#include <string.h>#include <stdio.h>#include <stdlib.h>int main(void){ //char data[] = "84 104 105 115 32 105 115 32 97 32 116 101 115 116 33"; char data[] = "66 97 99 107 32 116 111 32 116 101 120 116 33"; char tmp[64] = ""; char *p; p = data; for (int i = 0; i < sizeof(tmp); i++) { tmp[i] = strtol(p, &p, 10); if (!*p) break; } printf("Text: %s\n", tmp); return (0);}`
May the source be with you

• Guest
##### Re: Help with algorithm!
« Reply #8 on: February 23, 2011, 08:30:46 am »
It's VERY simple. Use the %c format in printf(), instead of %d.

Internally, the computer doesn't deal with letters, just numbers, and B's number is 66, so %c is all you need.

Do you know how to "walk" through the string, char by char?
« Last Edit: February 23, 2011, 08:35:13 am by Adak »

#### CommonTater

• Guest
##### Re: Help with algorithm!
« Reply #9 on: February 23, 2011, 04:26:15 pm »
Hey, Adak... good to see you here!