我想实现我自己的struct.pack
特定功能,将 IP 字符串(即“192.168.0.1”)打包为 32 位打包值,而不使用socket.inet_aton
内置方法。
我到目前为止:
ip = "192.168.0.1"
hex_list = map(hex, map(int, ip.split('.')))
# hex list now is : ['0xc0', '0xa8', '0x0', '0x01']
我的问题是:
我如何从那里得到['0xc0', '0xa8', '0x0', '0x01']
,'\xc0\xa8\x00\x01'
(这就是我从中得到的socket.inet_aton(ip)
?
(还有- 该字符串中间怎么可能有一个 NUL ( )?我想我对格式\x00
缺乏一些了解)\x