How to use the unpack function in the struct module in python

17 Views Asked by At

I am using unpack to parse a piece of data, this is a byte stream in it 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', I removed the first eight bytes, leaving 28 bytes, how should I parse them.The following is the data coding structure diagram. coding structure

This is my code:

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)

But always the error:

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

0

There are 0 best solutions below