How to convert base64 byte to string byte without extra slashes and b''

I want convert this base64 string to a byte string like this:

base64:

XHhjXHg0OFx4ODNceGU0XHgwXHhlOFx4Y2NceDAwXHgwMFx4MDBceDQxXA==

string byte:

\xc\x48\x83\xe4\x0\xe8\xcc\x00\x00\x00\x41\

I made this code:

import base64

bstring = "XHhjXHg0OFx4ODNceGU0XHgwXHhlOFx4Y2NceDAwXHgwMFx4MDBceDQxXA=="
decoded = base64.b64decode(bstring)
print(decoded)

And this is the output:

b'\\xc\\x48\\x83\\xe4\\x0\\xe8\\xcc\\x00\\x00\\x00\\x41\\'

So this is the problem. I don’t wanna double slash and b” so how can I convert it like this (I need byte): \xc\x48\x83\xe4\x0\xe8\xcc\x00\x00\x00\x41\?

>Solution :

Just run the .decode() method on the resulting bytestring:

import base64

bstring = "XHhjXHg0OFx4ODNceGU0XHgwXHhlOFx4Y2NceDAwXHgwMFx4MDBceDQxXA=="
decoded = base64.b64decode(bstring).decode()
print(decoded)  # => \xc\x48\x83\xe4\x0\xe8\xcc\x00\x00\x00\x41\

Leave a Reply