[C]
From: Fatted (obeseted_at_yahoo.com)
Date: 07/21/04
- Next message: jeffc: "Re: Should I migrate from C to C++?"
- Previous message: Roberto Dias: "menu-based console application"
- Next in thread: Mike Wahler: "Re: [C]"
- Reply: Mike Wahler: "Re: [C]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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);
}
- Next message: jeffc: "Re: Should I migrate from C to C++?"
- Previous message: Roberto Dias: "menu-based console application"
- Next in thread: Mike Wahler: "Re: [C]"
- Reply: Mike Wahler: "Re: [C]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|