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 strings that look like this:
10000010 00000000 00000000 00000000 and 10000010 00000000 00001111 11111111.
Then the script will convert them to ip addresses, so in this example, my script should return 130.0.0.0 and 130.0.15.255
This is my code so far:
def main():text=open('filename', 'r').readlines()for line in text:words = line.split(" ")words_2=list(words)for char in words:low_range=char.replace('*','0')conversion=str(int(low_range, 2))decimal='.'.join(map(str,conversion))print(decimal)for char in words_2:high_range=char.replace('*','1')conversion_2=str(int(high_range, 2))decimal='.'.join(map(str,conversion_2))print(decimal)
main()
When I run my code, it prints out:
1.3.0
0
0
0
1.3.0
0
6.3
2.5.5
1.3.0
0
6.4
0
1.3.0
0
9.5
2.5.5
1.3.0
0
1.2.8
0
1.3.0
0
1.9.1
2.5.5
1.3.0
0
1.3.0
0
1.9.2
0
1.3.0
0
2.5.5
2.5.5
When I really want it to print out:
130.0.0.0
130.0.63.255
130.0.64.0
130.0.95.255
130.0.128.0
130.0.191.255
130.0.192.0
130.0.255.255
Can anyone help explain what I am doing wrong?