我需要一个将用户输入的IPv4地址转换为二进制和基地址10的程序。事情是这样的:将点十进制IP地址转换为二进制(Python)
input: 142.55.33.1
output (base 10): [2385977601]
output (base 2): [10001110 00110111 00100001 00000001]
到目前为止,我已经成功地将其转换成一个base10地址,但我似乎无法得到解决的基础问题2:
#!/usr/bin/python3
ip_address = input("Please enter a dot decimal IP Address: ")
#splits the user entered IP address on the dot
ListA = ip_address.split(".")
ListA = list(map(int, ListA))
ListA = ListA[0]*(256**3) + ListA[1]*(256**2) + ListA[2]*(256**1) + ListA[3]
print("The IP Address in base 10 is: " , ListA)
#attempt at binary conversion (failing)
#ListA = ListA[0]*(2**3) + ListA[1]*(2**2) + ListA[2]*(2**1) + ListA[3]
#print("The IP Address in base 2 is: " , ListA)
任何帮助将不胜感激。谢谢。
非常感谢!这非常有帮助! – user1819786