简体   繁体   English

在python中以正确的格式打包和解压缩

[英]pack and unpack at the right format in python

I'm looking to unpack from a buffer a string and its length. 我正在寻找从缓冲区解包一个字符串及其长度。

For example to obtain (4, 'Gégé') from this buffer : 例如,要从此缓冲区获取(4, 'Gégé')
b'\\x00\\x04G\\xE9g\\xe9'

Does someone know how to do ? 有人知道该怎么做吗?

The length data looks like a big-endian unsigned 16 bit integer, and the string data looks like it's using the Latin1 encoding. 长度数据看起来像一个大端无符号16位整数,字符串数据看起来像是使用Latin1编码。 If that's correct, you can extract it like this: 如果正确的话,您可以像这样提取它:

from struct import unpack

def extract(buff):
    return unpack(b'>H', buff[:2])[0], buff[2:].decode('latin1')

buff = b'\x00\x04G\xE9g\xe9'
print(extract(buff))

output 产量

(4, 'Gégé')

Another possibility for the encoding is the old Windows code page 1252 , which can be decoded using .decode('cp1252') . 编码的另一种可能性是旧的Windows代码页1252 ,可以使用.decode('cp1252')对其进行解码。


The above code works in both Python 2 & Python 3. But in Python 3 there's an easier way: we don't need struct.unpack , we can use the int.from_bytes method. 上面的代码在Python 2和Python 3中都适用。但是在Python 3中,有一个更简单的方法:我们不需要struct.unpack ,我们可以使用int.from_bytes方法。

def extract(buff):
    return int.from_bytes(buff[:2], 'big'), buff[2:].decode('latin1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM