解压缩Python中的格式字符



我需要这个Perl字符串的Python模拟:

unpack("nNccH*", string_val)

我需要nNccH*数据格式的Python格式字符。

在Perl中,它将二进制数据解压为五个变量:

  • "网络"中的16位值(big-endian)
  • "网络"中的32位值(big-endian)
  • 带符号字符(8位整数)值
  • 带符号字符(8位整数)值
  • 十六进制字符串,高位半字节优先

但我在Python 中做不到

更多:

bstring = ''
while DataByte = client[0].recv(1):
    bstring += DataByte
print len(bstring)
if len(bstring):
    a, b, c, d, e = unpack("nNccH*", bstring)

我从来没有用Perl或Python编写过,但我目前的任务是编写一个用Perl编写的多线程Python服务器。。。

Perl格式"nNcc"等效于Python格式"!HLbb"。Perl的"H*"在Python中没有直接的等价物。

有两个问题。

  • Python的struct.unpack不接受通配符*
  • Python的struct.unpack不"hexlify"数据字符串

第一个问题可以使用像unpack这样的辅助函数来解决。

第二个问题可以使用binascii.hexlify:来解决

import struct
import binascii
def unpack(fmt, data):
    """
    Return struct.unpack(fmt, data) with the optional single * in fmt replaced with
    the appropriate number, given the length of data.
    """
    # http://stackoverflow.com/a/7867892/190597
    try:
        return struct.unpack(fmt, data)
    except struct.error:
        flen = struct.calcsize(fmt.replace('*', ''))
        alen = len(data)
        idx = fmt.find('*')
        before_char = fmt[idx-1]
        n = (alen-flen)//struct.calcsize(before_char)+1
        fmt = ''.join((fmt[:idx-1], str(n), before_char, fmt[idx+1:]))
        return struct.unpack(fmt, data)
data = open('data').read()
x = list(unpack("!HLbbs*", data))
# x[-1].encode('hex') works in Python 2, but not in Python 3
x[-1] = binascii.hexlify(x[-1])
print(x)

当在这个Perl脚本生成的数据上进行测试时:

$line = pack("nNccH*", 1, 2, 10, 4, '1fba');
print "$line";

Python脚本生成

[1, 2, 10, 4, '1fba']

您要查找的等效Python函数是struct.unpack。格式字符串的文档如下:http://docs.python.org/library/struct.html

如果你真的解释了你需要什么样的开箱,你会有更好的机会得到帮助。不是每个人都知道Perl。

最新更新