python - UnicodeDecodeError:“utf-8”编解码器无法解码位置 2 中的字节 0xf3:无效的继续字节
问题描述
我想将十六进制字符串转换为 ASCII,我尝试了多种方法来转换它,但每次都会抛出相同的错误。
编码:
hexFile = open('message', 'rb')
fileData = hexFile.read()
dataString = str(fileData, 'UTF-8')
bytes_object = bytes.fromhex(dataString)
ascii_string = bytes_object.decode("ASCII")
print(ascii_string)
错误:
第 130 行,在 dataString = str(fileData, 'UTF-8')
UnicodeDecodeError:“utf-8”编解码器无法解码位置 2 中的字节 0xf3:无效的继续字节
解决方案
你可以binascii
为此使用模块
import binascii
with open('message','rb') as f:
print(binascii.b2a_uu(f.read()).decode()) #decode will return string from bytes
但请记住,它最多只能做 45 个数据长度。更多你可以使用binascii.b2a_base64
. 见文档
推荐阅读
- java - 为什么我有这个错误?ArrayIndexOutOfBoundsException
- php - 在特定句子之间插入标签,保持原始大小写
- javascript - 在一个 div 中编写 HTML 标签,在另一个 div 中查看所见即所得
- c++11 - 管理单个内存块的分配器
- c# - Aspose 在 MS Powerpoint 中查找给定单词
- java - 如何在android中每24小时运行一次代码
- c# - 在 iis 上从字节数组加载图像失败
- ios - 邀请某人作为 Apple Developer Program 的管理员,但他无法访问 App Store Connect
- symfony - 实体中的 Symfony 4 自动装配不起作用
- php - contact_us.php 没有向我的网站相关邮件地址发送电子邮件