I'm reading a binary file (Python 3) and am trying to convert chunks with the help of the struct module.
f = open(fn, "rb")
try:
a=f.read(2)
...
When I use :
unpack("h",b'\x6b\x0a')
it gives me the expected
(2667,)
But I can't use that syntax on
b'6b0a'
even though:
print(type(b'6b0a'))
print(type(b'\x6b\x0a'))
gives the same type:
<class 'bytes'>
<class 'bytes'>
What am I mixing up? I think this used to work for me in Python 2.x.
3 Answers 3
b'\x6b\x0a' is two bytes: 0x6b 0x0a. b'6b0a' is four bytes: 0x36 0x62 0x30 0x61.
>>> binascii.unhexlify(b'6b0a')
b'k\n'
2 Comments
Inside bytes literals you may use 2 methods for specifying individual bytes:
- a direct ASCII character, e. g.
b'xyz', or - an escape sequence (starting with
\), e.g.b'\n123円\x56\\'
Of course, you may combine both methods in one bytes literal, e. g. b'xy\n\x56abc'
So your b'\x6b\x0a' and b'6b0a' a both the valid bytes literals - but unfortunately different:
- the 1st one consists of
2bytes (represented as escape sequences\x6band\x0a) - the 2nd one consists of
4bytes (represented as ASCII characters6,b,0, anda).
Comments
\x6b
inside a b'' is the standard string literal's escape sequence (allowed in bytes literals, too) for the hexadecimal representation of a single byte, while
6b
inside that bytes literal are two ASCII characters representing two bytes.
From Python documentation:
Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding). Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.
While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256.