Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 get b'\x64', without having to reconvert the int to bytes?

like image 370
glS Avatar asked Jan 11 '16 08:01

glS


1 Answers

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.

like image 171
Tom Karzes Avatar answered Oct 21 '22 17:10

Tom Karzes