python - 如何将字节数组转换为字符串?
问题描述
我刚刚完成了哈夫曼压缩算法的创建。我使用 bytearray() 将压缩文本从字符串转换为字节数组。我试图解压缩我的霍夫曼算法。我唯一担心的是我无法将我的字节数组转换回字符串。是否有任何内置函数可以用来将我的字节数组(带有变量)转换回字符串?如果没有,是否有更好的方法将我的压缩字符串转换为其他内容?我尝试使用 byte_array.decode() 并且得到了这个:
print("Index: ", Index) # The Index
# Subsituting text to our compressed index
for x in range(len(TextTest)):
TextTest[x]=Index[TextTest[x]]
NewText=''.join(TextTest)
# print(NewText)
# NewText=int(NewText)
byte_array = bytearray() # Converts the compressed string text to bytes
for i in range(0, len(NewText), 8):
byte_array.append(int(NewText[i:i + 8], 2))
NewSize = ("Compressed file Size:",sys.getsizeof(byte_array),'bytes')
print(byte_array)
print(byte_array)
print(NewSize)
x=bytes(byte_array)
x.decode()
UnicodeDecodeError:“utf-8”编解码器无法解码位置 0 中的字节 0x88:无效的起始字节
解决方案
推荐阅读
- python-3.x - 我如何获取 audiosrc URL?
- python - 带有嵌套括号的 Python 文本到嵌套列表
- python - BeautifulSoup 没有找到所有的标签
- r - 地点和栖息地的按物种比例/百分比堆积条形图
- c# - VirtualScreenWidth 尺寸不准确
- recursion - LISP 打印到同一行
- spring-boot - GKE 升级后,Google Kubernetes 部署上的 DSpace jspui 失败
- sql-server - 为什么 SSMS 中的“查询”窗格无法完全识别我的表?
- python - 有没有办法将图片链接标记为剧透 - Discord bot
- vim - 如何在 Neovim 的新选项卡中打开终端,只使用一个命令而不重新映射?