题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def reint(aa):
a = aa.split(".")
lenth = len(a)
b = []
for i in range(lenth):
b.append(bin(int(a[i])))
c = "".join(b)
d = c.split("0b")
e = []
for each in d:
temp = "0" * (8 - len(each)) + each
e.append(temp)
f = e[1:]
newd = "".join(f)
print(int(newd, 2))
def reip(bb):
s = bin(int(bb))
two = s[2:]
lenth = len(two)
ss = "0" * (32 - lenth) + str(two)
if len(ss) <= 32:
temp = (str(int(ss[0:8], 2))+ "."+ str(int(ss[8:16], 2))+ "."+ str(int(ss[16:24], 2))+ "."+str(int(ss[24:32], 2)))
print(temp)
#转换为2进制之后,最大32位
aa=input()
bb=input()
reint(aa)
reip(bb)
