Hi guy .
I don`t known how to convert IP to hex .
In this link http://www.mustat.com/50.22.100.250 (Ip host www.cplusplus.com) . I see hex IP : Hex IP 0x321664fa
I want to convert IP "50.22.100.250" to "0x321664fa"
Hi kbw
I try with your code
Ip 127.0.0.1 -> hex IP : 0x0100007f when i get hex ip from http://www.mustat.com/127.0.0.1 is 0x7f000001 . Result not correct .
Can you fix it ?
And hi andywestken , thank for your help , but use code from you , it`s wrong .
Internet Addresses
Values specified using the ".'' notation take one of the following forms:
a.b.c.d a.b.c a.b a
When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the 4 bytes of an Internet address. When an Internet address is viewed as a 32-bit integer quantity on the Intel architecture, the bytes referred to above appear as "d.c.b.a''. That is, the bytes on an Intel processor are ordered from right to left.
As the original poster appears to be using Windows (from the winsock.h), I thought I'd better mention that your code is Linux specific: WinSock uses network byte order (big endian, the same as Linux) rather than host byte order (little endian).
Tweaking your code to output the value from inet_addr as well, you can see the byte order is reversed.
As the original poster appears to be using Windows (from the winsock.h), I thought I'd better mention that your code is Linux specific: WinSock uses network byte order (big endian, the same as Linux) rather than host byte order (little endian).
Actually it's OP specific, since he was unsatisfied with the byte order returned by inet_addr and was apparently unable to figure out how to reorder it. After all, he only wanted to get the same string he was shown by the website he cited.
Well, as I already kind of mentioned, you do seem to be putting (tiny) stuff on the heap that is usually left on the stack. In this code, I would have put none of the variables on the heap.
So I'm curious where the habit comes from? Java?? Or???
My habit does not come from Java, i only work with C and C++.
I have always preferred allocate dynamic memory on the heap regardless of its size because it enables me to gain a thorough control of the memory consumption of the program. The memory on the stack offers a high performance but cannot be liberated when one wants to difference of the allocated on the heap as you know.
My obsession to reduce memory consumption when i doing work that turn this into my habit hence the small temporary variables such as i have written before the has allocated on the heap inside a function. This obsession happens to me also when i make structures, abuse of the bit fields to gain performance and decrease memory consumption of the structure.