首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python unpack 跟 php unpack区别

2013-04-26 
python unpack 和 php unpack区别$body unpack(Lcmd/Lstatus/a*username, $body_string)cnt unpack

python unpack 和 php unpack区别


$body = unpack("Lcmd/Lstatus/a*username", $body_string);



cnt = unpack('LLs', body_string)


同样的 
body_string php能解出来, python总是报错。

觉得 php Lcmd/Lstatus/a*username  与 python LLs 有区别 。 求解答? Python PHP
[解决办法]
看文档嘛,格式s不大一样,一般前面要带个数字表示字串长度,没有的话默认为1等同c不是你要的吧...

offset = struct.calcsize('2L')
cmd, status = struct.unpack('2L', body_string[:offset])
username = body_string[offset:]

或者:
fmt = '2L%ds' % (len(body_string) - struct.calcsize('2L'))
cmd, status, username = struct.unpack(fmt, body_string)

热点排行