【问题标题】:How to convert bytearray to string in python [duplicate]如何在python中将bytearray转换为字符串[重复]
【发布时间】:2018-04-30 06:01:50
【问题描述】:

我需要将下一个字节数组转换为字符串:

Num = bytearray()

我试过了

Num = bytearray(str) 

但这不是解决办法

【问题讨论】:

标签: python


【解决方案1】:

正如t.m.adam在cmets中所说,使用bytearray.decode

b = bytearray("test", encoding="utf-8")
b.decode()
#> 'test'

【讨论】:

  • 运行 b.decode() 而不考虑原始字节数组的编码(或者它是否甚至表示编码的 Unicode 文本)只会导致 UnicodeDecodeErrors 和编码错误。
猜你喜欢
  • 1970-01-01
  • 2021-06-11
  • 2016-06-04
  • 1970-01-01
  • 2018-06-11
  • 2018-08-31
  • 2020-10-27
  • 2019-12-30
  • 2021-08-16
相关资源
最近更新 更多