user97662
user97662

Reputation: 960

python 3.7 printing binary string shows strange character

in python 3.7, when I execute the following statement:

print(b'\x80\x51\x01\x00')

I get

b'\x80Q\x01\x00'

Why is that?

Upvotes: 0

Views: 855

Answers (1)

Taimander
Taimander

Reputation: 36

A binary string in Python always begins with b.

The Q is decoded because \x51 or 0x51 in ascii is Q.

In order to print a binary string, you first need to decode it with string.decode():

print(b'\x51\x52\x53'.decode('ascii'))

In the case of your string, it can not be decoded as ASCII because 0x80 is not a valid character. (ASCII only goes up to 0x7F)

Upvotes: 1

Related Questions