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?
struct. pack() is the function that converts a given list of values into their corresponding string representation. It requires the user to specify the format and order of the values that need to be converted. The following code shows how to pack some given data into its binary form using the module's struct.
Python struct pack_into(), unpack_from() These functions allow us to pack the values into string buffer and unpack from a string buffer. These functions are introduced in version 2.5. That's all for a short introduction of python struct module.
How about this?
struct.pack('>I', some_integer) + struct.pack('b'*len(long_array), *long_array)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With