Pelles C forum
C language => Work in progress => Topic started by: Freddy 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!"
But how to do it? Please help me with some source code!
Thanks!
-
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
-
#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);
}
-
Where you get it???
-
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 :P
#!/usr/local/bin/anch -run -Wall -g
#include <stdio.h>
#include <string.h>
#define MAX 100
char *input char *msg
static char s[MAX+1]
printf "%s",msg
while fgets(s,MAX,stdin) && !strchr(s,'\n')
return s
char *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 ret
int main void
char *s,*t
int i
s = input "Please enter a string:\n"
t = numText s
puts t
return 0
-
Hi hellork,
I guess you should create another topic to present the tool to the forum members.
-
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.
-
An another way:
#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);
}
-
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?
-
Hey, Adak... good to see you here!