Skip to content

Instantly share code, notes, and snippets.

@eriwen
Created July 14, 2010 21:16
Show Gist options
  • Save eriwen/476087 to your computer and use it in GitHub Desktop.
Save eriwen/476087 to your computer and use it in GitHub Desktop.
IP Address to hex
#!/usr/bin/env python
import sys, re
ip_regex = re.compile('(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})')
ip_match = ip_regex.match(sys.argv[1])
if (ip_match == None):
print 'Invalid address'
sys.exit(1)
hex_ip_addr = 0
for i in range(1,5):
hex_ip_addr += int(ip_match.group(i)) << (4-i)*8
print hex(hex_ip_addr).replace('0x', '').zfill(8).upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment