格式化字符的问题

时间:2014-06-02 18:04:03

标签: python struct

在显示字符时遇到一些问题

我从协议请求中收到了有效负载:

538cb9350404521a6c44020404563b152606102001085800020002aabb0000563b1526000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

长度为509

我要显示的是前4个字节,然后是1个字节,然后是1个字节

538cb935

04

04

目前正在查看我正在执行以下操作的有效负载:

tm = struct.unpack(">L", payload[0:4])
print "Time : ", tm
ouroripv = struct.unpack(">b", payload[5])
print "Our onion address : "    
print "Ip version : ", ouroripv
ouroraddrlen = struct.unpack(">b", payload[6]) # Giving a length of 82 etc atm 
print "Ip length : ", ouroraddrlen

我得到了结果:

Time :  (1401731381,)
Our onion address : 
Ip version :  (4,)
Ip length :  (82,)

正如你可以看到Ip长度,有效载荷上的第6个字节显示的是82而不是它应该是4,显示它需要的正确struct.unpack命令是什么?

我该怎么做?

谢谢你们

1 个答案:

答案 0 :(得分:2)

在python中,切片不包含最后一个值,因此payload[0:4]占用前4个字节,从0到3。

payload[3]是第四个字节

payload[4]是第五个字节