に戻って変換する:バイトコード化のPython 3にIPアドレスを、私のようなのpython3のサンプルUDPパケットヘッダーを作成しようとしている文字列
# "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
私が欲しいのは、2行目の値です。
192.168.1.1
どこが間違っているのですか?
[IPアドレスをPythonのバイトに変換する]の可能な複製(https://stackoverflow.com/questions/33244775/converting-ip-address-into-bytes-in-python) – thatrockbottomprogrammer
@thatrockbottomprogrammer私が苦労していることwithは文字列に戻ってipを変換しています – sgrew