将IP转换为16进制,用于IPv4-IPv6
# --*-- coding: utf-8 --*--
# create by xiaocaiji
while 1:
str_ip = input("input a IP:")
list_ip = str_ip.split('.')
if len(list_ip) < 4:
print("error IP")
for i in list_ip:
if int(i) > 256:
print("error IP")
elif int(i) < 0 :
print("error IP")
a = hex(int(list_ip[0]) * 256 + int(list_ip[1]))
b = hex(int(list_ip[2]) * 256 + int(list_ip[3]))
a_new = a.replace('0x','')
b_new = b.replace('0x','')
while len(a_new) < 4:
a_new = '0' + a_new
if len(a_new) == 4 :
break
# print(a_new)
while len(b_new) < 4:
b_new = '0' + b_new
if len(b_new) == 4 :
break
# print(b_new)
print("%s:%s"%(a_new.upper(),b_new.upper()))

更多精彩