Python Comparison of byte literals
Bytes can represent any number of things. Python cannot and will not guess at what your bytes might encode.
For example, int(b'0b11111111', 34)
is also a valid interpretation, but that interpretation is not equal to hex FF.
The number of interpretations, in fact, is endless. The bytes could represent a series of ASCII codepoints, or image colors, or musical notes.
Until you explicitly apply an interpretation, the bytes object consists just of the sequence of values in the range 0-255, and the textual representation of those bytes use ASCII if so representable as printable text:
>>> list(bytes(b'0b11111111'))
[48, 98, 49, 49, 49, 49, 49, 49, 49, 49]
>>> list(bytes(b'0xff'))
[48, 120, 102, 102]
Those byte sequences are not equal.
If you want to interpret these sequences explicitly as integer literals, then use ast.literal_eval()
to interpret decoded text values; always normalise first before comparison:
>>> import ast
>>> ast.literal_eval(b'0b11111111'.decode('utf8'))
255
>>> ast.literal_eval(b'0xff'.decode('utf8'))
255
b'0b11111111'
consists of 10 bytes:
In [44]: list(b'0b11111111')
Out[44]: ['0', 'b', '1', '1', '1', '1', '1', '1', '1', '1']
whereas b'0xff'
consists of 4 bytes:
In [45]: list(b'0xff')
Out[45]: ['0', 'x', 'f', 'f']
Clearly, they are not the same objects.
Python values explicitness. (Explicit is better than implicit.) It does not assume that b'0b11111111'
is necessarily the binary representation of an integer. It's just a string of bytes. How you choose to interpret it must be explicitly stated.