Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!samsung!uunet!unhd!jwm775 From: jwm775@uunet!unhd (Jonathan W Miner) Newsgroups: comp.lang.c Subject: Re: Converting ascii hex values to hex bytes Message-ID: <1990Oct23.220754.15958@uunet!unhd> Date: 23 Oct 90 22:07:54 GMT References: <298@cti1.UUCP> Reply-To: jwm775@unhd.UUCP (Jonathan W Miner) Distribution: comp Organization: Computing Information Services, University of New Hampshire Lines: 34 In article <298@cti1.UUCP> mpledger@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@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@unhd.unh.edu | speak for me! (603)868-3416 | Rather be downhill skiing... or hacking...