#!/usr/bin/python
# This script convert decimal IP address into hexadecimal, format "0xffffffff"
import sys
# User option is IPv4 address
if len(sys.argv)< 2:
print("Usage: %s (DEC FORMAT, for example 192.168.0.1)" % sys.argv[0])
sys.exit(0)
user_option = sys.argv[1]
ip_address = str(user_option)
# Function to convert IPv4 into HEX format
def convert_dec_hex(ip_address):
ip_address_hex="0x"+"".join([(hex(int(x))[2:].zfill(2)) for x in ip_address.split('.')])
return ip_address_hex
# Function to convert Ipv4 into BIN format
def convert_dec_bin(ip_address):
ip_address_bin=".".join([(bin(int(x))[2:].zfill(8)) for x in ip_address.split('.')])
return ip_address_bin
if __name__ == "__main__":
ip_hex=convert_dec_hex(ip_address)
#ip_bin=convert_dec_bin(ip_address)
print ip_hex
#print ip_bin
Testing:
$ ./ip_hex.py 10.0.0.1
0x0a000001
$ ./ip_hex.py 192.168.0.1
0xc0a80001