Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
IP address to binary conversion
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeroen Wolff  
View profile  
 More options Jun 13 2001, 2:57 pm
Newsgroups: comp.lang.python
From: Jeroen Wolff <jwo...@ision.nl>
Date: Wed, 13 Jun 2001 20:52:48 +0200
Local: Wed, Jun 13 2001 2:52 pm
Subject: IP address to binary conversion
Hi,

Can somebody help me (a newbee) to convert an ip address to is 32 bits
representation?

i would like to store these 32bits interger into a MYSQL database
together with the mask.

Jeroen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rikard Bosnjakovic  
View profile  
 More options Jun 13 2001, 8:00 pm
Newsgroups: comp.lang.python
From: Rikard Bosnjakovic <b...@hack.org>
Date: Thu, 14 Jun 2001 01:58:50 +0200
Local: Wed, Jun 13 2001 7:58 pm
Subject: Re: IP address to binary conversion

Jeroen Wolff wrote:
> Can somebody help me (a newbee) to convert an ip address to is 32 bits
> representation?

Lucky you, I had exactly that problem yesterday and hacked this
solution:

------------------[snip]------------------
import socket, struct

# return true if a string could be a pure ip address (4 dotted)
def is_ipaddress(str):
    if str.count(".") != 3:
        return 0
    for i in str.split("."):
        if not i.isdigit():
            return 0
        i = int(i)
        if i>255 or i<0:
            return 0
    return 1

def interpret_addr(str):
    host = None
    if is_ipaddress(str):
        res = socket.inet_aton(str)
    else:
        try:
            host = socket.gethostbyname(str)
        except:
            print "Can't resolve '%s'" %(str)
            return 0

    if host:
        res = socket.inet_aton(host)

    res = struct.unpack("!l", res)[0]

    if res == -1:
        return 0

    return res

>>> interpret_addr("10.30.40.50")
169748530
>>> interpret_addr("www.hotmail.com")

1074015239
------------------[snip]------------------

--
Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rikard Bosnjakovic  
View profile  
 More options Jun 13 2001, 8:02 pm
Newsgroups: comp.lang.python
From: Rikard Bosnjakovic <b...@hack.org>
Date: Thu, 14 Jun 2001 02:01:14 +0200
Local: Wed, Jun 13 2001 8:01 pm
Subject: Re: IP address to binary conversion
Here's a solution for the reverse:

>>> socket.inet_ntoa(struct.pack("!I", long(1074015239)))

'64.4.44.7'

--
Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeroen Wolff  
View profile  
 More options Jun 14 2001, 1:27 am
Newsgroups: comp.lang.python
From: Jeroen Wolff <jwo...@knoware.nl>
Date: Thu, 14 Jun 2001 07:14:07 +0200
Local: Thurs, Jun 14 2001 1:14 am
Subject: Re: IP address to binary conversion
On Thu, 14 Jun 2001 02:01:14 +0200, Rikard Bosnjakovic <b...@hack.org>
wrote:

>Here's a solution for the reverse:

>>>> socket.inet_ntoa(struct.pack("!I", long(1074015239)))
>'64.4.44.7'

Thanks for the solution!

But when i try: interpret_addr("195.7.145.2") --> -1022914302
the result is an signed long.
So the way back is not good....

Is there a way to make it unsigned? ( a 32 bits unsigned value needed)

Jeroen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rikard Bosnjakovic  
View profile  
 More options Jun 14 2001, 2:29 am
Newsgroups: comp.lang.python
From: Rikard Bosnjakovic <b...@hack.org>
Date: Thu, 14 Jun 2001 08:29:41 +0200
Local: Thurs, Jun 14 2001 2:29 am
Subject: Re: IP address to binary conversion

Jeroen Wolff wrote:
> Is there a way to make it unsigned? ( a 32 bits unsigned value needed)

Change "l" to "I" or "L" in the struct.unpack(), then you will get a
Python long integer.

--
Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Greg Jorgensen  
View profile  
 More options Jun 14 2001, 4:19 am
Newsgroups: comp.lang.python
From: "Greg Jorgensen" <gr...@pobox.com>
Date: Thu, 14 Jun 2001 08:19:57 GMT
Local: Thurs, Jun 14 2001 4:19 am
Subject: Re: IP address to binary conversion
"Jeroen Wolff" <jwo...@ision.nl> wrote in message

news:tedfit8li6m4i5gknj0749v5iriau5fo6q@4ax.com...

> Hi,

> Can somebody help me (a newbee) to convert an ip address to is 32 bits
> representation?

Each of the four numbers in the dotted-quad IP address is the decimal
version of a hex pair. It's already a 32-bit number, you just have to do a
little bit of work.

One way is to simply multiply the address out. For example, given the IP
address 192.168.2.1, the 32-bit equivalent is:

192 * 256^3 + 186 * 256^2 + 2 * 256 + 1 = 3232236033 (0xC0A80201)

Here's one Python solution. Note the long integers: Python's regular signed
integers aren't big enough.

>>> ip = "192.168.2.1"
>>> q = ip.split(".")
>>> n = reduce(lambda a,b: long(a)*256 + long(b), q)
>>> n

3232236033L

The reduce() is equivalent to:

>>> n = long(q[0]) * 256**3 + long(q[1]) * 256**2 + long(q[2]) * 256 +

long(q[3])

You can get a mask m bits long like this:

>>> mask = (long(2)**m) - 1

Now you can simply AND these together to get the network and host portions
of the address:

>>> host = n & mask
>>> net = n - host

Use the %x (or %X) format conversion to see the result in hex:

>>> print "net: %X  host: %X" % (net, host)

net: C0000000  host: A80201

Here's a trickier way to convert a decimal dotted-quad IP address to a hex
string and then to a decimal integer:

>>> # change each decimal portion of IP address to hex pair
>>> hexn = ''.join(["%02X" % long(i) for i in ip.split('.')])
>>> long(hexn, 16)

3232236033L

Enjoy.

Greg Jorgensen
PDXperts LLC
Portland, Oregon USA
gr...@pdxperts.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeroen Wolff  
View profile  
 More options Jun 15 2001, 2:42 am
Newsgroups: comp.lang.python
From: Jeroen Wolff <jwo...@ision.nl>
Date: Fri, 15 Jun 2001 08:31:18 +0200
Local: Fri, Jun 15 2001 2:31 am
Subject: Re: IP address to binary conversion

>>>> host = n & mask
>>>> net = n - host
>>>> print "net: %X  host: %X" % (net, host)
>net: C0000000  host: A80201

Is there a way back to present the calculated results in a doted
decimal notation?

Jeroen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rikard Bosnjakovic  
View profile  
 More options Jun 15 2001, 2:57 am
Newsgroups: comp.lang.python
From: Rikard Bosnjakovic <b...@hack.org>
Date: Fri, 15 Jun 2001 08:57:47 +0200
Local: Fri, Jun 15 2001 2:57 am
Subject: Re: IP address to binary conversion

Jeroen Wolff wrote:
> Is there a way back to present the calculated results in a doted
> decimal notation?

Greg's code:

>>> import socket, struct
>>> ip = "192.168.2.1"
>>> q = ip.split(".")
>>> n = reduce(lambda a,b: long(a)*256 + long(b), q)
>>> n

3232236033L

Back to dotted:

>>> socket.inet_ntoa(struct.pack('!I', n))

'192.168.2.1'

--
Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Martelli  
View profile  
 More options Jun 15 2001, 6:52 am
Newsgroups: comp.lang.python
From: "Alex Martelli" <alea...@yahoo.com>
Date: Fri, 15 Jun 2001 12:15:54 +0200
Local: Fri, Jun 15 2001 6:15 am
Subject: Re: IP address to binary conversion
"Jeroen Wolff" <jwo...@ision.nl> wrote in message

news:vqajitkao6tvll7ads8v84nbatp1hhr8jn@4ax.com...

> >>>> host = n & mask
> >>>> net = n - host

> >>>> print "net: %X  host: %X" % (net, host)
> >net: C0000000  host: A80201

> Is there a way back to present the calculated results in a doted
> decimal notation?
>>> import socket, struct
>>> print socket.inet_ntoa(struct.pack("!L",host))
0.168.2.1

Alex

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Greg Jorgensen  
View profile  
 More options Jun 16 2001, 5:22 am
Newsgroups: comp.lang.python
From: "Greg Jorgensen" <gr...@pobox.com>
Date: Sat, 16 Jun 2001 09:22:06 GMT
Local: Sat, Jun 16 2001 5:22 am
Subject: Re: IP address to binary conversion
"Jeroen Wolff" <jwo...@ision.nl> wrote in message

news:vqajitkao6tvll7ads8v84nbatp1hhr8jn@4ax.com...

> >>>> host = n & mask
> >>>> net = n - host

> >>>> print "net: %X  host: %X" % (net, host)
> >net: C0000000  host: A80201

> Is there a way back to present the calculated results in a doted
> decimal notation?

The posted solutions using socket/struct are fine. Here's another way:

---
def numToDottedQuad(n):
    "convert long int to dotted quad string"

    d = 256 * 256 * 256
    q = []
    while d > 0:
        m,n = divmod(n,d)
        q.append(str(m))
        d = d/256

    return '.'.join(q)
---

Greg Jorgensen
PDXperts LLC
Portland, OR, USA
gr...@pdxperts.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »