Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the byte string and turn it in to a character (Unicode) string.
Python 3:
encoding = 'utf-8'
b'hello'.decode(encoding)
or
str(b'hello', encoding)
Python 2:
encoding = 'utf-8'
'hello'.decode(encoding)
or
unicode('hello', encoding)
python - How to convert a byte array to string? - Stack Overflow
How to convert a bytes array literally to string instead of converting to string?
How do I convert a string to bytes?
How do i make a bytes object convert to a formatted string.
I want to convert an encrypted byte array into literal string for example:
Consider the below text as my bytes array:
b'x\xaa\xfc""|\xe2\xf7\xd1\xdaH\xf0\x8e\xa9>j>\x08\xd3\xad\xe2\xa3W\xdb\x1b\xd5\xa1\xb2q\x89V\xf0Zg\xd6a\x02\x12'
I want to convert it into:
'x\xaa\xfc""|\xe2\xf7\xd1\xdaH\xf0\x8e\xa9>j>\x08\xd3\xad\xe2\xa3W\xdb\x1b\xd5\xa1\xb2q\x89V\xf0Zg\xd6a\x02\x12'
The same bytes array into string format.
I am unable to phrase this question properly to search in google, thought I can get help to either solve this issue or to understand how to phrase this issue.
P.S: First post in this sub, If I made any mistakes please do point it out.
Edit: If anyone wants to know why I need it, just consider that the function I am using only takes string as input and to convert that function such that it will use the encrypted byte array as input is a big hassle and I want the encrypted byte array to stay as in encrypted format instead of decoding it.
Suppose I something like
s = "GW\x25\001"
How do I convert that string to bytes, interpreting the backslashes as escapes? In other words, the resulting byte array should be of length 4.
UPDATE: Hmmm, I was taking the string from sys.argv[1], which seems to complicate things and not make it turn out as expected. So I'm still not sure what the answer is.