172.18.16.154
and spit out it's hex equiv:
AC12109A
Thanks in advance...
--
Keith Kaple x25759 -----------------------
| New distribution, $40. Linux compatible
| sound card, $89. Three button mouse, $18.
| Nuking windows partitions....priceless.
char * IP_to_hex_str(char * ip_str)
{
unsigned int hex_1;
unsigned int hex_2;
unsigned int hex_3;
unsigned int hex_4;
char * hex_str;
if (!ip_str)
return NULL;
hex_str = malloc(9);
if (!hex_str)
return NULL;
sscanf(ip_str, "%d.%d.%d.%d",
&hex_1, &hex_2, &hex_3, &hex_4);
sprintf(hex_str, "%02X%02X%02X%02X",
hex_1, hex_2, hex_3, hex_4);
return hex_str;
}
The above function is untested.
By the way, shouldn't the hex equivalent be: AC.12.10.9A ?
--
Thomas Matthews
email: mat...@stamps.stortek.com
const char *ip_addr_string = "172.18.16.154";
unsigned long ip_addr = 0;
unsigned char *p = (void *) &ip_addr;
unsigned ip_addr_arr[4];
sscanf (ip_addr_string, "%u.%u.%u.%u",
ip_addr_arr + 3,
ip_addr_arr + 2,
ip_addr_arr + 1,
ip_addr_arr + 0);
p[0] = ip_addr_arr[0];
p[1] = ip_addr_arr[1];
p[2] = ip_addr_arr[2];
p[3] = ip_addr_arr[3];
printf ("%lX\n", ip_addr);
Modulo the byte order of your machine, of course.
--
James C. Hu <j...@cs.wustl.edu> Computer Science Doctoral Candidate
http://www.cs.wustl.edu/~jxh/ Washington University in Saint Louis
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>
>Looking for a piece of code that will take the string:
>172.18.16.154
>and spit out it's hex equiv:
>AC12109A
Two lines, using the standard libarary functions sscanf() and
sprintf() should be all you need. Have a look at "%d" and "%x"
format specifiers
Kurt
--
| Kurt Watzka
| wat...@stat.uni-muenchen.de
>Keith Kaple wrote:
>>
>> Looking for a piece of code that will take the string:
>>
>> 172.18.16.154
>>
>> and spit out it's hex equiv:
>>
>> AC12109A
>>
>> Thanks in advance...
>char * IP_to_hex_str(char * ip_str)
>{
> unsigned int hex_1;
> unsigned int hex_2;
> unsigned int hex_3;
> unsigned int hex_4;
> char * hex_str;
> if (!ip_str)
> return NULL;
> hex_str = malloc(9);
> if (!hex_str)
> return NULL;
Be sure to mention in the documentation for this function that it
returns a pointer to dynamically allocated memory, and that the
_caller_ will be responsible for calling free() at the right time.
> sscanf(ip_str, "%d.%d.%d.%d",
> &hex_1, &hex_2, &hex_3, &hex_4);
Is there a specific reason to use "%d" as a format string when the
variables are of type "unsigned int"?