Question

Anyone know how the MySql INET_NTOA() is converting the integer value back to IP address format? I would like to know how the calculation is performed internally to convert the IP to its original internet standard dotted-decimal format.

Was it helpful?

Solution

It decomposes an integer into four bytes using network byte order (big endian):

167773449 => A0 00 05 09

It then joins those bytes together with a dot in between:

10.0.5.9

OTHER TIPS

Writing 16777343 in Binary:
1 0000 0000 0000 0000 0111 1111
Start grouping the bits (in groups of 8 to form a byte) starting from the LSB:
[1] [0000 0000] [0000 0000] [0111 1111]
And now writing in decimal in little endian form(Least significant byte first):
127.0.0.1

This can be verified by using :

struct sockaddr_in antelope;
inet_aton("127.0.0.1", &antelope.sin_addr); // store IP in antelope
printf("%lld\n",antelope.sin_addr.s_addr); //prints 16777343
printf("%s\n",inet_ntoa(antelope.sin_addr)); //prints 127.0.0.1

I hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top