0
我試圖創建Python3樣品UDP數據包報頭這樣:字節編碼在Python 3的IP地址,並轉換回字符串
# "encoding"
header = bytearray()
ip = '192.168.1.1'
ip_bytes = bytes(map(int, ip.split('.')))
header.extend(ip_bytes)
port = 5555
header.extend(port.to_bytes(2, 'big'))
print(header)
print()
# "decoding"
destip = header[:4]
ips = ""
for i in destip:
byt = int.from_bytes(destip[i:i+1], 'big')
ips += str(byt) + "."
ips = ips[:len(ips)-1]
print(ips)
,輸出是:
bytearray(b'\xc0\xa8\x01\x01\x15\xb3')
bytearray(b'\xc0\xa8\x01\x01')
0.0.168.168
我想要的第二行是:
192.168.1.1
任何人都知道我要去哪裏錯了?
可能重複的[在python中將IP地址轉換爲字節](https://stackoverflow.com/questions/33244775/converting-ip-address-into-bytes-in-python) – thatrockbottomprogrammer
@thatrockbottomprogrammer我在掙扎與正在將IP BACK轉換爲字符串 – sgrew