题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
while 1: try: ip_address = list(map(int, input().split('.'))) ip_number = int(input(), 10) # 先做ip地址转十进制 ip_bin = '' for i in range(len(ip_address)): bin_str = '0' * (8 - len(bin(ip_address[i])[2:])) + bin(ip_address[i])[2:] ip_bin = ip_bin + bin_str ip_bin = '0b' + ip_bin ip_10 = int(ip_bin, 2) print(ip_10, end='\n') # 再做十进制转ip地址 ip_list = [] ip_10 = '0' * (32 - len(bin(ip_number)[2:])) + bin(ip_number)[2:] for j in range(4): bin_str = '0b' + ip_10[j*8:(j+1)*8] b = str(int(bin_str, 2)) ip_list.append(b) print('.'.join(ip_list)) except: break