NO

Author Topic: Help with algorithm!  (Read 11678 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!"

But how to do it? Please help me with some source code!

Thanks!

Offline Robert

  • Member
  • *
  • Posts: 245
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 :P

Code: [Select]
#!/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

Offline Vortex

  • Member
  • *
  • Posts: 797
    • http://www.vortex.masmcode.com
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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
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

Adak

  • 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!