题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def IP_to_10(ip):
ip = ip.split('.')#列表
longint = []
for i in ip:
er = bin(int(i))[2:].rjust(8,'0')#字符串
longint.append(er)#列表元素为字符串
print(int(''.join(longint),2))
def to_IP(ip1):
IP = []
er = bin(ip1)[2:].rjust(32,'0')
IP.append(str(int(er[0:8],2)))
IP.append('.')
IP.append(str(int(er[8:16],2)))
IP.append('.')
IP.append(str(int(er[16:24],2)))
IP.append('.')
IP.append(str(int(er[24:],2)))
print(''.join(IP))
while 1:
try:
IP_to_10(input())
to_IP(int(input()))
except:
break