Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have a 4-bytes bytearray in Python, andI would like to extract two unsigned short int. bytes[0] and bytes[1] for the first number, and bytes[2] and bytes[3] for the second.

This is what I have for the moment:

bytes_payload = bytearray(string_payload)

print bytes_payload[0]

print bytes_payload[1]

print bytes_payload[2]

print bytes_payload[3]

How can I do that?

Thanks

1 Answer

0 votes
by (25.1k points)

You can use the struct.unpack("HH", bytes_payload)

Like this:

unpacked = struct.unpack("HH", bytes_payload)

print(unpacked[0])

print(unpacked[1])

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...