Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert bytearray to string in python [duplicate]

Tags:

python

I need to convert the next bytearray to string:

Num = bytearray()

I have tried

Num = bytearray(str) 

but that's not the solution

like image 508
Bian Avatar asked Apr 30 '18 06:04

Bian


People also ask

How do I turn a list into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

Which method is used to convert raw byte data to a string in Python?

Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A byte string can be decoded back into a character string, if you know which encoding was used to encode it.


1 Answers

As t.m.adam said in the comments, use bytearray.decode

b = bytearray("test", encoding="utf-8")
b.decode()
#> 'test'
like image 159
Increasingly Idiotic Avatar answered Oct 02 '22 17:10

Increasingly Idiotic