题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def intiptrans(s):
iplst=s.split('.')
num=''
for item in iplst:
item=bin(int(item))
item=item[2:]
item=item.rjust(8,'0')
num+=item
num=int(num,2)
return num
def binaryiptrans(s):
num=bin(int(s))[2:]
num=num.rjust(32,'0')
iplst=[]
tag=0
ip=''
for i in range(4):
iplst.append(num[tag:tag+8])
tag+=8
iplst[i]=str(int(iplst[i],2))
ip='.'.join(iplst)
return ip
s1=input()
print(intiptrans(s1))
s2=input()
print(binaryiptrans(s2))