Python Convert Binary String to IP Address in Dotted Notation

2024/10/12 18:21:15

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?

Answer

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)
https://en.xdnf.cn/q/118172.html

Related Q&A

Scrapy: Extracting data from source and its links

Edited question to link to original:Scrapy getting data from links within tableFrom the link https://www.tdcj.state.tx.us/death_row/dr_info/trottiewillielast.htmlI am trying to get info from the main t…

Rename file on upload to admin using Django

I have used a function in Django 1.6 to rename my files when they are uploaded through admin, but this does not work in Django 1.8. Anyone know if it is still possible to do this in 1.8?class Entry(mo…

Ignore newline character in binary file with Python?

I open my file like so :f = open("filename.ext", "rb") # ensure binary reading with bMy first line of data looks like this (when using f.readline()):\x04\x00\x00\x00\x12\x00\x00\x00…

RegEx Parse Error by Parsley Python

I have made a simple parser for simple queries, to fetch data from a datastore. The operands I have used are <,<=,>,>=,==,!= The Parser works fine for every operand except for < I am a b…

Accessing Bangla (UTF-8) string by index in Python

I have a string in Bangla and Im trying to access characters by index.# -*- coding: utf-8 -*- bstr = "তরদজ" print bstr # This line is working fine for i in bstr:print i, # question marks …

Computing KL divergence for many distributions

I have a matrix of test probability distributions:qs = np.array([[0.1, 0.6], [0.9, 0.4] ])(sums up to 1 in each column) and "true" distribution:p = np.array([0.5, 0.5])I would like to calcula…

Expanding mean over multiple series in pandas

I have a groupby object I apply expanding mean to. However I want that calculation over another series/group at the same time. Here is my code:d = { home : [A, B, B, A, B, A, A], away : [B, A,A, B, A, …

Moving window sum on a boollean array, with steps.

Im struggling with creating a moving window sum function that calculates the number of True values in a given numpy Boolean array my_array, with a window size of n and in jumping steps of s.For example…

Python - take the time difference from the first date in a column

Given the date column, I want to create another column diff that count how many days apart from the first date.date diff 2011-01-01 00:00:10 0 2011-01-01 00:00:11 0.000011 …

(Django) Limited ForeignKey choices by Current User

Update Thanks to Michael I was able to get this to work perfectly in my CreateView, but not in the UpdateView. When I try to set a form_class it spits out an improperly configured error. How can I go a…