| headshot (23) | |||
|
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"
Can you help me ? Thank guy. | |||
|
|
|||
| kbw (5375) | |||
Just print it as hex as in:
| |||
|
|
|||
| headshot (23) | |||
|
No no , It`s wrong Did you try IP "123.30.128.10" , IP hex : 0x7b1e800a When i try "%x" , it`s wrong
And this is result 7b 1e 80 and a further we must separate each octect by a dot., Then convert decimal to hexa | |||
|
Last edited on
|
|||
| cire (1852) | ||||
Looks right to me. Just because your presentation is a little bit off doesn't make the results wrong. | ||||
|
|
||||
| kbw (5375) | |||
| |||
|
|
|||
| andywestken (1950) | |||
Or, as the original post, can use the WinSock 2 version, though it should be winsock2.h rather than winsock.h
Andy PS also see inet_addr function http://msdn.microsoft.com/en-us/library/windows/desktop/ms738563%28v=vs.85%29.aspx (inc. sample code and comment near the end.) Andy | |||
|
|
|||
| headshot (23) | |
|
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 . Thank | |
|
|
|
| Cubbi (1583) | |||
Why all those complications when there's asio?
online demo: http://liveworkspace.org/code/d742b41b06b8fe925e4a3f95e62c4cd2 | |||
|
|
|||
| headshot (23) | |||
|
oh Hi Cubbi . It`s work in online But in my C++ , not found file #include <boost/asio.hpp> , and i get errors at line
I use window 7 , VS C++ 2005 ,2010 Thank you | |||
|
|
|||
| naraku9333 (923) | ||
Quote from the page andywestken posted.
| ||
|
|
||
| kbw (5375) | |||||
I guess, you know best. | |||||
|
|
|||||
| Cubbi (1583) | ||
Then you can install boost from http://www.boostpro.com/download/ | ||
|
|
||
| JAL (5) | |||||
Here, this work.
Example:
| |||||
|
|
|||||
| cire (1852) | |||
That seems a little overly complicated, Jal.
| |||
|
|
|||
| andywestken (1950) | ||||
|
@cire 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.
(where the to_uint32_t() value precedes the inet_addr() value)
Andy PS @Jal - I'n curious where your code comes from. Is it your? It's rather unusual to see a single integer allocated on the heap! (or string!) int* Current_Quantity = new int();
| ||||
|
Last edited on
|
||||
| cire (1852) | ||
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. | ||
|
|
||
| andywestken (1950) | |
|
@cire oops! Sorry, I lost the thread at some point... Andy | |
|
|
|
| JAL (5) | |
|
@andywestken Yes, it is mine, in fact i often work as well. | |
|
|
|
| andywestken (1950) | |
|
@Jal 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??? Andy | |
|
|
|
| JAL (5) | |||
|
@andywestken 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. Example:
| |||
|
|
|||