Convert any IPv4 or IPv6 address to its decimal (integer) representation and back. Useful for database storage, range calculations, and network programming.
An IPv4 address is four octets separated by dots, where each octet is a number from 0โ255. Internally, routers and databases often store IP addresses as a single 32-bit unsigned integer (decimal). Converting between the dotted-decimal and integer formats is useful for database storage, subnet math, and firewall rule optimisation.
For an IP like 192.168.1.1: multiply the first octet by 2ยฒโด, the second by 2ยนโถ, the third by 2โธ, and add the fourth directly. So 192ร16777216 + 168ร65536 + 1ร256 + 1 = 3232235777.
Storing IPs as integers uses 4 bytes instead of up to 15 bytes for the string form, and allows fast range queries with BETWEEN clauses.
Bitwise AND of an IP integer with a subnet mask integer immediately yields the network address โ the basis of all CIDR routing logic.
The conversion is perfectly reversible. Divide the integer by successive powers of 256 to extract each octet back to dotted-decimal form.
255.255.255.255 converts to 4,294,967,295 โ the maximum value of a 32-bit unsigned integer.
IPv6 addresses are 128-bit, so their integer representation is a very large number (up to 340 undecillion). The same principle applies but you need 128-bit arithmetic.