如何在python中将bytearray转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50094765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:23:44  来源:igfitidea点击:

How to convert bytearray to string in python

python

提问by Bian

I need to convert the next bytearray to string:

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

Num = bytearray()

I have tried

我试过了

Num = bytearray(str) 

but that's not the solution

但这不是解决方案

回答by Increasingly Idiotic

As t.m.adamsaid in the comments, use bytearray.decode

正如tmadam在评论中所说,使用bytearray.decode

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