Created
July 14, 2010 21:16
-
-
Save eriwen/476087 to your computer and use it in GitHub Desktop.
IP Address to hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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