I need the Python analog for this Perl string:
unpack("nNccH*", string_val)
I need the nNccH* – data format in Python format characters.
In Perl it unpack binary data to five variables:
- 16 bit value in “network” (big-endian)
- 32 bit value in “network” (big-endian)
- Signed char (8-bit integer) value
- Signed char (8-bit integer) value
- Hexadecimal string, high nibble first
But I can’t do it in Python
More:
bstring = ''
while DataByte = client[0].recv(1):
bstring += DataByte
print len(bstring)
if len(bstring):
a, b, c, d, e = unpack("nNccH*", bstring)
I never wrote in Perl or Python, but my current task is to write a multithreading Python server that was written in Perl…
The Perl format
"nNcc"is equivalent to the Python format"!HLbb".There is no direct equivalent in Python for Perl’s
"H*".There are two problems.
struct.unpackdoes not accept the wildcard character,*struct.unpackdoes not “hexlify” data stringsThe first problem can be worked-around using a helper function like
unpack.The second problem can be solved using
binascii.hexlify:When tested on data produced by this Perl script:
The Python script yields