Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!lll-winken!unixhub!shelby!neon!Gang-of-Four!dkeisen From: dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) Newsgroups: comp.lang.c Subject: Re: Converting ascii hex values to hex bytes Message-ID: <1990Oct17.162529.20096@Neon.Stanford.EDU> Date: 17 Oct 90 16:25:29 GMT References: <298@cti1.UUCP> Sender: news@Neon.Stanford.EDU (USENET News System) Distribution: comp Organization: Sequoia Peripherals Lines: 39 In article <298@cti1.UUCP> mpledger@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@Gang-of-Four.Stanford.EDU Office: (415) 967-5644 1447 N. Shoreline Blvd. Mountain View, CA 94043