Convert IP to Int and Int to IP

Submitted by:Pitter Alderson

Date added:23 September, 2013

Category:Python

Convert an IP address to an integer for easier storage. Also, convert an integer back to an IP address.

Tags: convert ip , int to ip

Code Snippet:

    def IntToDottedIP( intip ):
octet = ''
for exp in [3,2,1,0]:
octet = octet + str(intip / ( 256 ** exp )) + "."
intip = intip % ( 256 ** exp )
return(octet.rstrip('.'))

def DottedIPToInt( dotted_ip ):
exp = 3
intip = 0
for quad in dotted_ip.split('.'):
intip = intip + (int(quad) * (256 ** exp))
exp = exp - 1
return(intip)
 
 

Comments