Question

I'm trying to convert a Perl script to python, and it uses quite a few different packs. I've been able to figure out the lettering differences in the "templates" for each one, but I'm having an issue with understanding how to handle Perl's lack of length declaration.

example:

pack('Nc*',$some_integer,$long_array_of_integers);

I don't see an analog for this "*" feature in struct.pack, on Python. Any ideas on how to convert this to Python?

Was it helpful?

Solution

How about this?

struct.pack('>I', some_integer) + struct.pack('b'*len(long_array), *long_array)

OTHER TIPS

Perl's pack is using the '*' character similar to in regular expressions--meaning a wildcard for more of the same. Here, of course, it means more signed ints.

In Python, you'd just loop through the string and concat the pieces:

result = struct.pack('>L', some_integer)
for c in long_array_of_integers:
    result += struct.pack('b',c)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top