Skip to content

Instantly share code, notes, and snippets.

@michaelkeevildown
Created June 9, 2016 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelkeevildown/6b3f04f5405698803406172d10a5d255 to your computer and use it in GitHub Desktop.
Save michaelkeevildown/6b3f04f5405698803406172d10a5d255 to your computer and use it in GitHub Desktop.
IP to BigInt
def ip_to_bigint(ip):
ip_parts = ip.split('.')
part_1 = int(ip_parts[0]) * 16777216
part_2 = int(ip_parts[1]) * 65536
part_3 = int(ip_parts[2]) * 256
part_4 = int(ip_parts[3])
bigint_ip = part_1 + part_2 + part_3 + part_4
print 'IP Address to BigInt: ' + str(bigint_ip)
def bigint_to_ip(bigint_ip):
part_1 = bigint_ip / 16777216
rest_of_ip = bigint_ip - (part_1 * 16777216)
part_2 = rest_of_ip / 65536
rest_of_ip = rest_of_ip - (part_2 * 65536)
part_3 = rest_of_ip / 256
part_4 = rest_of_ip - (part_3 * 256)
ip_address = str(part_1) + '.' + str(part_2) + '.' + str(part_3) + '.' + str(part_4)
print 'BigInt to IP Address: ' + ip_address
# Set example ip addresses
ip = '127.0.0.1'
bigint_ip = 2130706434
# Call functions
ip_to_bigint(ip)
bigint_to_ip(bigint_ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment