## Converting a decimal number to ip address in python

So, I was messing around with something tonight where I needed to get the ip address of a machine from decimal format into the traditional string as we know it. Now, an ip address is literally just a 32 bit number really, and thus the ip address. 192.168.0.1 converts into 11000000101010000000000000000001 in binary, or 3232235521 in decimal.

So just how do we go about converting this. Well for most of you this is going to be easy, but I for one had never messed with binary data much before in python and so I thought I would give this a try.

My code below works around two loops, one nested inside the other. Why you ask? Well, we need to eventually get 4 numbers out of this, to make up our, w.x.y.z format. Since each octet is 8 bits long, 32 / 8 = 4. Man I can’t remember the last time maths was that easy.

So we loop through the outer loop 4 times. The inner loop, 8 times. This is because we are actually recreating the binary notation from the decimal, splitting the decimal into 4 distinct 8bit binary numbers. I could have gotten the whole number out as a 32bit value and padded it with zeros on the front if it didn’t quite hit 32 bits and then use some fancy string manips to pull out 8 characters at a time.

I didn’t want to do it that way though 🙂 This way we get to learn about a cool operator in python, two in fact. As we step through the first loop we immediately come to a second, where you see the line `ip1=str(ipint % 2)+ip1`

So what does this actually do. Well most of it should be familiar to you. The only bit which may cause a question is the % operator. It’s called the modulus operator. Basically it divides the first number by the second and gives us what’s left over, instead of giving us a decimal figure. Whenever we calculate the modulus of a number against the value of 2, we can only have one of two possibly outcomes, either the number is even or odd, so the leftover is either 1 or 0. In essence that tells us the last “bit” of the binary number. So we add that to a string, making sure we put it at the right hand end.

Next we need to look at the next bit. We do this by using the >> operator. This shifts the bit one position to the right, essentially making the number one bit smaller and chopping off the last bit that we have just identified using the modulus function.

We continue this procedure 8 times, building up a string, which will contain the binary representation of our last octet. We then add that to another string using the `int(ip1,2)`

command, which turns the binary back into a decimal. We’ve then hit the end of our inner loop. We continue this for three more times, and at the end we print out our final ip address.

Simple really.

```
```

```
ipint = 3232235521
ip=""
for i in range(4):
ip1 = ""
for j in range(8):
print ipint % 2
ip1=str(ipint % 2)+ip1
ipint = ipint >> 1
print ip1
print ip1
ip = str(int(ip1,2)) + "." + ip
print ip.strip(".")
```

```
```

import socket

socket.inet_ntoa(packed_ip)

See http://docs.python.org/library/socket.html#socket.inet_ntoa