Как использовать функцию распаковки в модуле структуры в PythonPython

Программы на Python
Anonymous
Как использовать функцию распаковки в модуле структуры в Python

Сообщение Anonymous »


Я использую unpack для анализа фрагмента данных, это поток байтов

Код: Выделить всё

b'\xc0\x0f\x00\x00\x00\x00\x00\x00\xe8\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
, я удалил первые восемь байтов, оставив 28 байтов, как мне их проанализировать. Ниже представлена ​​диаграмма структуры кодирования данных.
[img]https:// i.stack.imgur.com/kxRDT.png[/img]
Это мой код:

Код: Выделить всё

import struct
hex_string = "c0 0f 00 00 00 00 00 00 e8 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 e8 03 00 00"

hex_bytes = bytes.fromhex(hex_string.replace(" ", ""))
print(len(hex_bytes))
print(struct.calcsize("IIIQQ"))

length, message_type, data_count, cumulative_count, timestamp = struct.unpack('IIIQQ', hex_bytes[:28])

print("Length:", length)
print("Message Type:", message_type)
print("Data Count:", data_count)
print("Cumulative Count:", cumulative_count)
print("Timestamp:", timestamp)
Но всегда ошибка:

Код: Выделить всё

struct.error: unpack requires a buffer of 32 bytes
I've looked up a lot of information in the evening, but I can't solve it. If anyone can help, thank you in advance


Источник: https://stackoverflow.com/questions/781 ... -in-python

Вернуться в «Python»