mkaz.blog

Fun with IP Addresses

It is possible to represent an IPv4 address in different formats than the common x.x.x.x notation. An IPv4 address is simply a 32-bit number, or a 4-byte number with each section representing a byte.For example using my mkaz.com IP address: 45.79.78.169Converting each part to binary:

DecimalBinaryHex
45001011012D
79010011114F
78010011104E
16910101001A9

All together the binary number would be: 00101101010011110100111010101001Converting the full number to the decimal works as http://760172201/You can also use the hexadecimal prefixing with 0x: http://0x2D4F4EA9/To convert a standard IP address to a single decimal, use the formula:a.b.c.d: (a * 256^3) + (b * 256^2) + (c * 256^1) + (d * 256^0)For above example, showing my work:

(45 * 256^3) + (79 * 256^2) + (78 * 256^1) + (169 * 256^0)
(45 * 16777216) + (79 * 65536) + (78 * 256) + (169 *1)
754974720 + 5177344 + 19968 + 169
760172201

As expected, giving the same number above.

Python Functions

The python REPL makes it relatively easy to do the quick conversions. For reference, here are the Python functions I used to help convert from different numerical bases.Convert decimal to binary:

>>> bin(45)
'0b101101'

Convert decimal to hex:

>>> hex(45)
'0x2d'

Convert binary to decimal:

>>> print( int('00101101010011110100111010101001', 2) );
760172201