简体   繁体   中英

Function byte encode same input and have different responses

How is this possible? I'm using python3.

a= b"\xfc\x48\x83\xe4\xf0\xe8"
b= "\xfc\x48\x83\xe4\xf0\xe8"
if (a == b.encode('ascii',errors='replace')):
      print ("Winner")

print (a)
b'\xfcH\x83\xe4\xf0\xe8'
print (b)
üHäðè

I've tried different type of errors but nothing. If I don't put any error, it pop ups an error that It says codec can't encode character '\xfc' in position 0: ordinal not in range(128) . I have read the manual onthe official manual but I can't find any good answer.

I would like to know how to convert (force) B to be as A. Same print, same ouput.

EDIT: I found the solution . I wanted to convert from hex string to bytes. Finally what I did was to replace "\x" from the string. This leaves the string with HEX characters. Then I used the bytes function bytes.fromhex().

b = b.replace("\\x", "")
b = bytes.fromhex(b)

From the documentation you linked:

Encodings don't have to handle every possible Unicode character, and most encodings don't. For example, Python's default encoding is the 'ascii' encoding. The rules for converting a Unicode string into the ASCII encoding are simple; for each code point:

If the code point is < 128, each byte is the same as the value of the code point.

If the code point is 128 or greater, the Unicode string can't be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.)

It looks like the first code point \xfc > 128 which means you cannot represent it in ascii encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM