Skip to content Skip to sidebar Skip to footer

How To Get A Single Byte In A String Of Bytes, Without Converting To Int

I have a string of bytes like str_of_bytes = b'\x20\x64\x20', of which I want to extract, say, the second element. If I do str_of_bytes[1], what I get is the int 100. How do I just

Solution 1:

Extract it as a range:

str_of_bytes[1:2]

Result:

b'd'

This is the same as b'\x64'

Note that I'm assuming you're using Python 3. Python 2 behaves differently.

Post a Comment for "How To Get A Single Byte In A String Of Bytes, Without Converting To Int"