Converting ascii hex values to hex bytes

Jonathan W Miner jwm775 at uunet!unhd
Wed Oct 24 08:07:54 AEST 1990


In article <298 at cti1.UUCP> mpledger at cti1.UUCP (Mark Pledger) writes:
>[... deleted stuff ...]
>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
>[... more deleted stuff ...]

I have not tested this but you should be able to take the string
"122.10.10.44" and run it through a sscanf to get four integers.  Then
stick the four integers into a character array.
       
        int  one,two,three,four;
        char ch[4]; 

        strcpy(s,"122.10.10.44\0");
 	sscanf(s,"%d.%d.%d.%d",&one,&two,&three,&four);
        ch[0] = one;
        ch[1] = two;
        ch[2] = three;
        ch[3] = four; 

The array 'ch' will now hold the values 7a,0a,0a,2c.  This should work
if your machine supports direct conversion from integers to characters.	

-----------------------------------------------------------------
Jonathan Miner        | I don't speak for UNH, and UNH does not 
jwm775 at unhd.unh.edu   | speak for me! 
(603)868-3416         | Rather be downhill skiing... or hacking... 
-- 
-----------------------------------------------------------------
Jonathan Miner        | I don't speak for UNH, and UNH does not 
jwm775 at unhd.unh.edu   | speak for me! 
(603)868-3416         | Rather be downhill skiing... or hacking... 



More information about the Comp.lang.c mailing list