Home > Net >  How to decode b"\x80" in python?
How to decode b"\x80" in python?

Time:05-21

I want to decode b"\x80" in python (I want to receive "\x80"). When I tried b"\x80".decode(), it gives me UnicodeDecodeError.
But when I tried just "\x80", it gives me what I expected. Why it's happened and how to get what I expect?

CodePudding user response:

You can use the latin-1 encoding to get the result you want.

>>> b"\x80".decode('latin-1')
'\x80'

This is a 8-bit encoding that covers \x00 to \xff. The first 256 code points of Unicode are based on latin-1 (aka ISO/IEC 8859-1)

You can also use encoding unicode_escape, which even works with higher unicode escape codes such as b"\u0080" and b"\U00000080".

>>> b"\u0080".decode('unicode_escape')
'\x80'
  • Related