Python Basics Packing and Unpacking

preview_player
Показать описание
Learn how to use packing and unpacking techniques with python programming
Рекомендации по теме
Комментарии
Автор

the best simple explanation, thank you

khirooo
Автор

Thank you. Indeed a very simple quick demo of pack and unpack concept.

upbge_codeart
Автор

I'd love to use this technique to plot data from a file! The plan is to prompt the user for file location, the number of bytes per data (1, 2, or 4), and the endiness (whether the first byte is the MSB or the LSB).

I understand to open a binary file is:
fileContent= open("sample.bin", "rb")

But how can I import all the file's data into an array? For example, let's say the file has 2 bytes and is UNSIGNED, and the MSB is the first byte, and LSB the second byte.. how would does the unpack change based on endiness?
a = struct.unpack("H" * ((len(fileContent)) // 2), fileContent)

Similarly, if the file has 4 bytes per data, is SIGNED, but the LSB is the FIRST byte, and btye #4 is the MSB(sign)?
a = struct.unpack("I" * ((len(fileContent)) // 4), fileContent)

bennguyen