Converting ascii hex values to hex bytes
Dave Eisen
dkeisen at Gang-of-Four.Stanford.EDU
Thu Oct 18 02:25:29 AEST 1990
In article <298 at cti1.UUCP> mpledger at cti1.UUCP (Mark Pledger) writes:
>and fwrite() to get a lot of different configuration data. What I want to
>do is to be able to convert 122.10.10.44 into a character string of 4 bytes
>that equals "\x7a\x0a\x0a\x2c". How can I convert the ascii representation
>into hex? I tried using itoa(), but the result is in ascii character format
You don't want to convert anything into hex, you want to convert the
character string "122.10.10.44" into four one-byte integers, char
being a synonym for one-byte integers. Something like this
will do it:
int
convert_data (const char *ascii, char *four_bytes)
{
char *p;
int bytenum;
for (bytenum = 0, p = ascii; bytenum < 4; bytenum++)
{
four_bytes[bytenum] = atoi (p);
if ((p = strchr (p, '.')) == NULL) /* strchr is called index in BSD */
return -1;
}
return 0;
}
Hex is only meaningful when you go to output the four bytes of data and
you need to convert it into a hex string.
--
Dave Eisen Home: (415) 323-9757
dkeisen at Gang-of-Four.Stanford.EDU Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043
More information about the Comp.lang.c
mailing list