1

I want to convert an ip address read from a file in the decimal format (192.168.65.72) to one in binary format {110000001010100001000001010001000}.I am reading the ip address in the decimal format from a file.Find the code snippet below.

/*contains 192.168.65.72*/
filter = open("filter.txt", "r")

for line in filter:
    bytePattern = "([01]?\d\d?|2[0-4]\d|25[0-5])"
    regObj = re.compile("\.".join([bytePattern]*4))
    for match in regObj.finditer(line):
        m1,m2,m3,m4 = match.groups()
        print "%s %s %s %s" %(m1, m2, m3, m4)

I want to convert m1,m2,m3 and m4 each into an 8bit binary value.I cannot figure out a way to do that.I am new to python.Any help will be greatly appreciated.

Cheers.

1
  • 2
    I'm guessing that you either want the IP address as a plain old 32-bit integer, or you want 4 bytes of data holding an IP address. I suspect you don't actually want a string containing 32 1's and 0's. Commented Aug 12, 2010 at 6:23

5 Answers 5

5
''.join([ bin(int(x))[2:].rjust(8,'0') for x in '123.123.123.123'.split('.')])
Sign up to request clarification or add additional context in comments.

6 Comments

Be careful about required padding. Each byte in the IP needs to be represented by 8 bit, regardless of the value.
''.join([bin(256 + int(x))[3:] for x in '123.123.123.123'.split('.')])
@Omnifarious bin(256 + int(x) this is one god damn good idea :) +1
I am a python newbie.I tried the above thing but it gives NameError: name 'bin' is not defined. I googled bin function in python and came to know that it was a part of the python standard library.do i need to import any other package to get the python standard library? Also what is the variable x supposed to mean? in the above explanation.It may not be the most intelligent question in the world but I do not understand it. Thank very much in advance. Cheers, liv2hak
1. No,you need not import anything to get bin. 2. What is used in above answer is called list comprehension. You can read docs.python.org/tutorial/…. If you don't understand something please let me know. Best of luck!
|
4

Convert an IPv4 address from dotted-quad string format (for example, ‘123.45.67.89’) to 32-bit packed binary format:

socket.inet_aton("192.168.65.72")

2 Comments

It returns '\xc0\xa8AH'
@avi: Yes that's correct. That's a string literal representing the 4 bytes. I recommend you switch to Python3, which should more correctly return a bytes literal instead.
2

If you want a string containing the raw data for the 32 bit IP address:

import struct
struct.pack('4B', *(int(x) for x in '123.123.123.123'.split('.')))

Or if you want a 32-bit integer containing the IP address:

import struct
struct.unpack('>I', struct.pack('4B', *(int(x) for x in '123.123.123.123'.split('.'))))

Comments

1
>>> bin( 192 )
'0b11000000'

String manipulation does the rest. Note however that in an IP all but the first parts are allowed to be zero.

Comments

0

You can use the function clean_ip() from the library DataPrep if you read your file into a DataFrame. Install DataPrep with pip install dataprep.

from dataprep.clean import clean_ip
df = pd.DataFrame({"ip": ["192.168.65.72", "123.123.123.123"]})

df2 = clean_ip(df, "ip", output_format="binary")
# print(df2)
                ip                          ip_clean
0    192.168.65.72  11000000101010000100000101001000
1  123.123.123.123  01111011011110110111101101111011

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.