@Barry Kelly and @Andrew Hare, in fact, I do not think that multiplication is the clearest way to do this (everything is correct).
Int32 "formatted" IP address can be considered as the following structure
[StructLayout(LayoutKind.Sequential, Pack = 1)] struct IPv4Address { public Byte A; public Byte B; public Byte C; public Byte D; }
So, to convert the IP address 64.233.187.99, you can do:
(64 = 0x40) << 24 == 0x40000000 (233 = 0xE9) << 16 == 0x00E90000 (187 = 0xBB) << 8 == 0x0000BB00 (99 = 0x63) == 0x00000063 ---------- =| 0x40E9BB63
so you can add them with + or you could use binairy or together. The result is in 0x40E9BB63, which is 1089059683. (In my opinion, looking in hexadecimal is much easier to see bytes)
So you can write a function as:
int ipToInt(int first, int second, int third, int fourth) { return (first << 24) | (second << 16) | (third << 8) | (fourth); }
Davy Landman Jan 21 '09 at 8:41 2009-01-21 08:41
source share