convert IP address hex to decimal in python:
Python script to convert hexadecimal value of IPv4 address into decimal:
#!/usr/bin/python
import sys
# Checking options, must be (0xXXXXXXXX)
if len(sys.argv)< 2:
print("Usage: %s (HEX FORMAT, for example 0xC0A80001)" % sys.argv[0])
sys.exit(0)
user_option = sys.argv[1]
hex_data=user_option[2:]
#Check length, should be 8 , leading 0 is matter
if len(hex_data)< 8:
hex_data = ''.join(('0',hex_data))
def hex_to_ip_decimal(hex_data):
ipaddr = "%i.%i.%i.%i" % (int(hex_data[0:2],16),int(hex_data[2:4],16),int(hex_data[4:6],16),int(hex_data[6:8],16))
return ipaddr
result=hex_to_ip_decimal(hex_data)
print result
Testing:
./decode_hex_to_ip.py 0xC0A80001
192.168.0.1
No comments:
Post a Comment