Equivalent Operation to b"" for an already defined string Python

Viewed 54

I am able to define a bytes string as follows:

string = b"0x340xEF"

I want to cast an already made string. Let us call it string into a bytes string. How do I perform this? bytes(string) is not giving me an equivalent result.

1 Answers

You need to encode the string to be able to convert it to bytes:

string = "b0x340xEF".encode("latin-1")
print(string)

Output:

b'b0x340xEF'
Related