在Python中解压缩格式字符

Sir*_*r D 4 python perl unpack

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

unpack("nNccH*", string_val)
Run Code Online (Sandbox Code Playgroud)

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

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

  • "网络"中的16位值(big-endian)
  • "网络"中的32位值(big-endian)
  • 有符号的char(8位整数)值
  • 有符号的char(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)
Run Code Online (Sandbox Code Playgroud)

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

unu*_*tbu 7

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

有两个问题.

  • 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)
Run Code Online (Sandbox Code Playgroud)

在对此Perl脚本生成的数据进行测试时:

$line = pack("nNccH*", 1, 2, 10, 4, '1fba');
print "$line";
Run Code Online (Sandbox Code Playgroud)

Python脚本产生

[1, 2, 10, 4, '1fba']
Run Code Online (Sandbox Code Playgroud)


Eli*_*sky 6

您正在寻找的等效Python函数是struct.unpack.格式字符串的文档位于:http://docs.python.org/library/struct.html

如果你真的解释了你需要什么样的拆包,你将有更好的机会获得帮助.不是每个人都知道Perl.