IP address to binary conversion

Greg Jorgensen gregj at pobox.com
Sat Jun 16 05:22:06 EDT 2001


"Jeroen Wolff" <jwolff at ision.nl> wrote in message
news:vqajitkao6tvll7ads8v84nbatp1hhr8jn at 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
gregj at pdxperts.com






More information about the Python-list mailing list