Network Addressing

Integer to IPv4

Direction
32-bit integer
IPv4 address 192.168.1.1
Unpack four bytes from 32 bits
Octets 192 · 168 · 1 · 1
Hexadecimal 0xC0A80101
Binary, by octet 11000000 10101000 00000001 00000001
Integer 3232235777
Note
Four shift-and-mask operations
Advertisement
320 × 100

Unpack the 32-bit integer into four bytes, most significant first. 3232235777 becomes 192.168.1.1. Anything above 4294967295 is out of range for IPv4.

How to convert an integer to an IP

1 Enter the 32-bit integer.
2 Read the dotted quad, with the octets, the hex and the 32 bits alongside.
3 If your number is negative, add 4294967296 first: it came out of a signed column.
4 Anything outside 0 to 4294967295 is refused.

The unpacking is four shift-and-mask operations: shift right by 24, 16, 8 and 0, masking each with 255. Seeing it written that way makes the structure obvious; the address really is one number, and the octets are just four slices of it.

A negative integer is the commonest reason this refuses an input, and the fix is arithmetic rather than a different tool. Signed 32-bit storage cannot hold anything from 128.0.0.0 upwards, so those addresses come back wrapped: −1062731519 plus 4294967296 is 3232235777, which is 192.168.1.1. Nothing about a negative number says which convention produced it, so it is refused rather than assumed.

Byte order is the other way this goes wrong quietly. Read the slices in the wrong direction and 192.168.1.1 becomes 1.1.168.192. Valid, plausible, and a different machine entirely. Addresses travel over the network big-endian while most processors are little-endian, which is what ntohl and htonl exist to reconcile. If a decoded address looks almost familiar, try reversing the octets before deciding the data is corrupt.

Questions

192.168.1.1.

Advertisement
300 × 250
Was this tool any good?
Internal signal only · I use it to find the tools worth rebuilding