# Convert decimal number to ipv4 format

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.

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

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

Out[74]: 168427520

So you could do something like this

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

• I am not using any language. I want to convert it on a paper with a pen. Commented Sep 18, 2014 at 19:41
• To convert 1023 into IP format first convert it to binary (here is a guide if you are unfamiliar with how to wikihow.com/Convert-from-Decimal-to-Binary) You'll get 1111110101 and as a 32 bit number it would be 00000000 00000000 00000011 11110101 Then convert each octet into decimal which will give you 0.0.3.255 Commented Sep 18, 2014 at 20:12
• Thanx a lot. Could you tell me how to add 122.13.17.34 and 0.0.3.255 both are in ipv4 format. Commented Sep 18, 2014 at 20:35
• 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 Commented Sep 18, 2014 at 21:01
• Though the method is a liitle bit lengthy but works perfectly and smoothly. Thanx again. Commented Sep 19, 2014 at 1:12

If you're doing it by computer, obviously it's done in binary. But given you want to do it by hand ...

You add the numbers directly in base 256. Just as we learned to add base 10 or base 12, we write the numbers in columns worth 1, 256, 256², 256³. When you add a column and it goes over 255, you have to carry. It looks ugly because base-256 digits take more than one character to write.

First convert 1023₁₀ to base 256: divide by appropriate powers of 256, keep remainder: 1023₁₀ = [3][255]₂₅₆

256³  256²  256     1     column values

|     |     |   3 | 255 |
| 122 |  13 |  17 |  34 |
-------------------------
|     |     |   1 |     |   this is the carry
| 122 |  13 |  21 |  33 |

The answer is then in dotted notation: 122.13.21.33.

Jonathan