Converting ascii hex values to hex bytes

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Thu Oct 18 15:32:19 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

void convert_internet_address_to_binary(const char *inet_addr, char *bin)
    {
	int i[4];
	sscanf(inet_addr, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
	bin[0] = i[0], bin[1] = i[1], bin[2] = i[2], bin[3] = i[3];
    }

Alas, the *scanf() family supports int*, short*, and long* arguments,
but not char*, which is why we need i[].
-- 
Fear most of all to be in error.	-- Kierkegaard, quoting Socrates.



More information about the Comp.lang.c mailing list