1

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.

2 Answers 2

4

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

6
  • I am not using any language. I want to convert it on a paper with a pen. Sep 18, 2014 at 19:41
  • 1
    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 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. 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 Sep 18, 2014 at 21:01
  • Though the method is a liitle bit lengthy but works perfectly and smoothly. Thanx again. Sep 19, 2014 at 1:12
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.