How to Convert Decimal Number to IPv4 Format

ipv4

I want to convert 1023 into ipv4 format because I want to add this number to another number in ipv4 format.
Can anyone help to sort it out.

Best Answer

If you are using python you can do it simply with the IPAddress object from the netaddr library.

In [68]: from netaddr import IPAddress
In [69]: IPAddress(1023)
Out[69]: IPAddress('0.0.3.255')

To get an integer value of an IPAddress object you can do the following

In [74]: IPAddress("10.10.0.0").value
Out[74]: 168427520

So you could do something like this

In [75]: IPAddress("10.10.0.0").value + IPAddress(1023)
Out[75]: IPAddress('10.10.3.255')

Edit: If you just have pen and paper, here is how you do it.

You'll have to convert both to decimal form, add the two and convert from decimal to binary. When you convert 122.13.17.34 to binary break each octet out and convert that to binary, which will give you 01111010.00001101.00010001.00100010 then remove the dots can you get 01111010000011010001000100100010. Now convert that to a decimal number and you'll get 2047676706. Now 2047676706 + 1023 = 2047677729. Convert that back to binary and you'll get 01111010000011010001010100100001. Now breaking that up into octets and just convert those octets back to decimal and you'll get 122.13.21.33

Related Topic