Working with bytes and binary data in Python

Posted by ignoramus on Stack Overflow See other posts from Stack Overflow or by ignoramus
Published on 2010-04-06T18:51:00Z Indexed on 2010/04/06 18:53 UTC
Read the original article Hit count: 445

Filed under:
|
|

Four consecutive bytes in a byte string together specify some value. However, only 7 bits in each byte are used; the most significant bit is ignored (that makes 28 bits altogether). So...

b"\x00\x00\x02\x01"

would be 000 0000 000 0000 000 0010 000 0001.

Or, for the sake of legibility, 10 000 0001. That's the value the four bytes represent. But I want a decimal, so I do this:

>>> 0b100000001
257

I can work all that out myself, but how would I incorporate it into a program?

© Stack Overflow or respective owner

Related posts about python

Related posts about bytes