Reputation: 448
This might be a simple question. I'm reading a binary MIDI file using Python 3 and I'm having problems understanding how to turn the hex value \x00\x00\x00\x06
into an integer, as I don't know what to do with the slash \
. To get this value I'm using:
with open("/path/to/midi/file.mid", "rb") as f:
header_chunk = f.read(4)
length = f.read(4)
length
ends up having a bytes object with value b"\x00\x00\x00\x06"
. This value can be the unpacked into the corresponding numbers using struct.unpack("cccc", length)
but this returns a tuple with hexadecimal numbers containing the \
, (b'\x00', b'\x00', b'\x00', b'\x06')
. Is there a built-in way to turn this numbers as they are, in bulk like in length
or in an individual fashion, to integers without having to deal with the \
manually?
Upvotes: 2
Views: 292
Reputation: 95957
Yeah, sure:
>>> length
b'\x00\x00\x00\x06'
>>> list(length)
[0, 0, 0, 6]
Note, if you access the individual elements of your bytes
object, you get an int
in return:
>>> length[0]
0
>>> length[1]
0
>>> length[2]
0
>>> length[3]
6
>>>
Upvotes: 1