Hi guy .
I don`t known how to convert IP to hex .
In this link http://www.mustat.com/70.42.23.121 (Ip host codeguru.com) . I see hex IP : Hex IP 0x462a1779
I want to convert IP "70.42.23.121" to "0x462a1779"
And this is result
7b 1e 80 and a , not 0a
further i think we must separate each octect by a (dot) . , Then convert decimal to hexa
Can you help me ?
Thank guy.
Last edited by S_M_A; September 27th, 2012 at 12:20 PM.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
I don't understand your ip_to_hex() function. If you want to convert a string with the ip in this format "70.42.23.121" then your function should take a string and not an integer.
headshot, you don't seem to understand the difference between integers, characters or strings. I think you need to start with the basics and then get to more complicated things.
Code:
unsigned int ip_to_hex(const char* strip)
{
unsigned int ip = 0;
char* str = _strdup(strip);
char delimiters[] = ".";
char* token = strtok(str, delimiters);
int tokencount = 0;
while(token != NULL)
{
if(tokencount < 4)
{
int n = atoi(token);
ip = ip | n << 8 * (3 - tokencount);
}
tokencount++;
token = strtok(NULL, delimiters);
}
free(str);
if(tokencount != 4)
ip = 0;
return ip;
}
int _tmain(int argc, _TCHAR* argv[])
{
int ip = ip_to_hex("127.0.0.1");
return 0;
}
Bookmarks