简体   繁体   English

Python:如何将十六进制字符串转换为普通字符串?

[英]Python: How to convert a hexadecimal string to a normal string?

I have a string of the following form我有以下形式的字符串

\xNN\xNN\xNN\xNN… \xNN\xNN\xNN\xNN…

  • N can be any digit from 0 to 9. For example: N 可以是 0 到 9 之间的任何数字。例如:
str = "\x41\x42\x43"
  • \xNN is a hexadecimal number that represents a character according to ASCII code. \xNN 是一个十六进制数,根据 ASCII 码表示一个字符。

Is there a simple way to convert this type of string to a normal string?有没有一种简单的方法可以将这种类型的字符串转换为普通字符串? For example "\x41\x42\x43" is equivalent to "ABC".例如“\x41\x42\x43”等价于“ABC”。

How about怎么样

>>> s = b"\x41\x42\x43"
>>> print(s)
b'ABC'

Or或者

>>> s = "\x41\x42\x43"
>>> print(s.encode())
b'ABC'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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