python-3.x - 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->
>>>
解决方案
您需要b
在字符串之前有前缀以表示它代表字节,而hex
不是编码,对于 ascii,您将使用.decode('ascii')
.
推荐阅读
- machine-learning - 使用随机森林作为带有 adaboost 的基础分类器
- flutter - Flutter GridView builder,每行具有动态高度和统一子级
- php - 导入类的 SmartGit 冲突错误
- countdowntimer - 通过代码在 Lightspeed 中添加倒数计时器
- firebase-storage - 如何在 Firebase 存储上按时间限制文件上传
- c++ - 向上或向下看时剔除截头锥体变窄
- python - 有限替换的python随机选择
- java - 自定义 jpackager(wix 工具集)
- c# - 构建解决方案时需要存在 .csproj nuget 包
- flutter - 当用户切换到新帐户时,如何停止对以前帐户的所有正在进行的呼叫?