[C]

From: Fatted (obeseted_at_yahoo.com)
Date: 07/21/04


Date: Wed, 21 Jul 2004 16:21:11 +0200

Consider the code below, where I'm converting from string of text to a
string of the (hex) ascii values of the text.
E.G. "sausages" -> "7361757361676573"

Where should I free the memory for char string ascii in convert2ascii()?
Or is there a better algorithm for going about this? (maybe pass a
pointer to a string containing the result as an argument of the function?)

Thanks,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 

int main(void)
{
        char *result;
        char *convert2ascii(char *);

        result = convert2ascii("sausages");
        printf("Res: %s",result);
        return(0);
}

char *convert2ascii(char *txt)
{
         char tmp[3];
         char *ascii;
 

         if((ascii = malloc((strlen(txt)*2)+1)) == NULL)
         {
                 perror("Malloc PWD");
                 exit(EXIT_FAILURE);
         }
 

         do
         {
                 sprintf(tmp,"%x",*txt++);
                 strcat(ascii,tmp);
         } while(*txt);
 
 
 

         return(ascii);
}



Relevant Pages

  • Re: healp reading / writing binary strings.
    ... Robert Klemme wrote: ... > And how can I write the same type of string from say an ... to the ascii table as well? ... for hex, does the hex value I supply correspond to the ascii table as ...
    (comp.lang.ruby)
  • Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
    ... converts it to it's Hex value and then converts the Hex value ... back to the Ascii string value. ... // system.in reader (the input from console) ...
    (comp.lang.java.help)
  • Posting Large Data to Querystring
    ... Arraylist that is converted into a string of hex values. ... just converting it to a string of hex values (which doubles the ... PS. the hash needs to be URL Safe. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
    ... It takes console input of an ascii ... converts it to it's Hex value and then converts the Hex value ... back to the Ascii string value. ...
    (comp.lang.java.help)
  • Deutsch nach ASCII konvertieren
    ... "ascii", $string, sub { ... also das to_ascii offenbar einen Latin-1-kodierten String will. ... Gefunden habe ich weiterhin eine kleine Sub, die die Ersetzung ... Ähnlich wie bei Variante 1 wird hier die Menge der Ersetzungs-Regexen ...
    (de.comp.lang.perl.misc)