1

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.

asked Nov 10, 2017 at 2:21

3 Answers 3

2

b'\x6b\x0a' is two bytes: 0x6b 0x0a. b'6b0a' is four bytes: 0x36 0x62 0x30 0x61.

>>> binascii.unhexlify(b'6b0a')
b'k\n'
answered Nov 10, 2017 at 2:26
Sign up to request clarification or add additional context in comments.

2 Comments

Ty. How do I convert b'6b0a' into b'\x6b\x0a?
Ty. Didn't see that because of the representation as ascii, which was my problem to begin with. So import binascii print(unpack("h", binascii.unhexlify(b'6b0a'))) --> ((2667,) works.
2

Inside bytes literals you may use 2 methods for specifying individual bytes:

  1. a direct ASCII character, e. g. b'xyz', or
  2. 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 2 bytes (represented as escape sequences \x6b and \x0a)
  • the 2nd one consists of 4 bytes (represented as ASCII characters 6, b, 0, and a).
answered Nov 10, 2017 at 3:42

Comments

1
\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.

answered Nov 10, 2017 at 2:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.