首页 > 解决方案 > python3 如何转换成我认为的 4 - '\x04'

问题描述

我有这个 '\x04'

如何转换为 ASCII 或其他格式?我不是二进制专家。这是十六进制吗?那是4的十六进制吗?

我尝试了以下方法:

b'\x01'.decode('hex')

print ('\x01'.decode('hex')  )
AttributeError: 'str' object has no attribute 'decode'

这不起作用:

~/Documents/workspace/project/ansible$ python3
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test = b'\x01'.decode('ascii') 
>>> print ("where->", test )
where-> 
>>> 

标签: python-3.x

解决方案


您需要b在字符串之前有前缀以表示它代表字节,而hex不是编码,对于 ascii,您将使用.decode('ascii').


推荐阅读