免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3018 | 回复: 4
打印 上一主题 下一主题

unpack 求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-31 12:15 |只看该作者 |倒序浏览
有个网络数据包要解包,本来以为这样两句话可以搞定
        for data in fileListData:
            print len(data)
            formatstr=(len(data)/50)*"33scII8s"
            templist=struct.unpack(formatstr,data)
可是它提示格式化字符串和数据长度不匹配,最后化成这样写才通过了,我看不出来这是什么区别?
        for data in fileListData:
            print len(data)
            formatstr=(len(data)/50)*"50s"
            templist=struct.unpack(formatstr,data)
            formatstr="33sc16s"
            formatstr2="II8s"
            for i in templist:
                #print i
                temp2=struct.unpack(formatstr,i)
                temp3=struct.unpack(formatstr2,temp2[2])
                self.ncfileInfo.append((temp2[0],temp2[1],temp3[1],temp3[0]))
数据我保证都是50的倍数长,抓包检验过的。

论坛徽章:
0
2 [报告]
发表于 2010-05-31 12:42 |只看该作者
给个数据吧。

论坛徽章:
0
3 [报告]
发表于 2010-05-31 12:56 |只看该作者
(len(data)/50)*"33sc16s"能不能通过?会不会是对齐的影响?
format  I  有个notes(3)
To align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero.

论坛徽章:
0
4 [报告]
发表于 2010-06-22 15:10 |只看该作者
python的unpack有点不一样,你传过来的有可能是用别的语言pack的吧,perl还是php还是其它的,最好再把系统环境说一下

论坛徽章:
0
5 [报告]
发表于 2010-06-22 17:39 |只看该作者
手册里面写得明白……
  1. >>> s.calcsize('33scII8s')
  2. 52
复制代码
  1. >>> s.calcsize('=33scII8s')
  2. 50
  3. >>> s.calcsize('!33scII8s')
  4. 50
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP