Skip to content Skip to sidebar Skip to footer

Convert Stringed Bytes Back Into Bytes

I'm working on a project which saves some bytes as a string, but I can't seem to figure out how to get the bytes back to a actual bytes! I've got this string: 'b'\x80\x03]q\x00(X\r

Solution 1:

Try:

x="b'\x80\x03]q\x00(X\r\x00\x00\x00My First Noteq\x01X\x0e\x00\x00\x00My Second Noteq\x02e.'"

y=x[2:-1].encode("utf-8")

>>> print(y)

b'\xc2\x80\x03]q\x00(X\r\x00\x00\x00My First Noteq\x01X\x0e\x00\x00\x00My Second Noteq\x02e.'>>> print(type(y))

<class'bytes'>

You have just bytes converted to regular string without encoding - so you have redundant tags indicating that: b'...' - you just need to drop them and python will do the rest for you ;)

Solution 2:

in python 3:

>>>a=b'\x00\x00\x00\x00\x07\x80\x00\x03'>>>b = list(a)>>>b
[0, 0, 0, 0, 7, 128, 0, 3]
>>>c = bytes(b)>>>c
b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>>

Post a Comment for "Convert Stringed Bytes Back Into Bytes"