Skip to content

Instantly share code, notes, and snippets.

@zebbra2014
Forked from eriwen/ip_to_hex.py
Created February 1, 2016 03:25
Show Gist options
  • Select an option

  • Save zebbra2014/5edc84dbd8bb62b6f5d3 to your computer and use it in GitHub Desktop.

Select an option

Save zebbra2014/5edc84dbd8bb62b6f5d3 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