Skip to content Skip to sidebar Skip to footer

Python Convert Binary String To Ip Address In Dotted Notation

So I'm trying to read in a file with some binary strings, i.e: 10000010 00000000 0000**** ********. The script will convert the *'s to both 0 and 1, so there will be two binary str

Solution 1:

You are joining the letters of byte's decimal representation, while you should join the bytes themselves.

decimal='.'.join(map(str,conversion))

Also you print each byte of an ip on its own line

print(decimal)

Here's how I'd write the loop:

for line in text:
    words = line.split(" ")

    for bit in '01':        
        ip = []
        for word in words:
            byte=word.replace('*', bit)
            ip.append(str(int(byte, 2)))
        print '.'.join(ip)

Solution 2:

You can use simple formatting:

"%d.%d.%d.%d" % (0b10000010, 0b00000000, 0b00000000, 0b00000000)
# '130.0.0.0'

To read a binary number from a string (say "10000010") use:

int("10000010", 2)
# 130

If you want to work with ip addresses I suggest using ipaddress:

>>> import ipaddress
>>> ipaddress.IPv4Address("%d.%d.%d.%d" % (0b10000010, 0b00000000, 0b00000000, 0b00000000))
IPv4Address('130.0.0.0')

However, it's not available under python 2.x

Solution 3:

Your example splits the input by whitespace to variable words. Then you iterate over the words, convert them to int and back to string to variable conversion. That all makes sense.

The problem is on following line:

decimal='.'.join(map(str,conversion))

When it get's executed for the first time the value is '130'. The map call is unnecessary, it just turns the conversion to list of strings: ['1', '3', '0']. Then you join the strings together with . so you get 1.3.0 that you seen in the output. Note that you'd get the same result even if you'd remove map since then join would just iterate over characters in the string.

Instead of iterating over the characters just convert every word with int just like you're doing and collect them to a list. Then convert them to strings with either map or list comprehension and finally join them together with ..

Here's a short example that does it:

withopen('test.txt') as text:
    for line in text:
        print'.'.join(str(int(x, 2)) for x in line.split())

Post a Comment for "Python Convert Binary String To Ip Address In Dotted Notation"