I have a python byte object like
JavaScript
x
2
1
a = b'1'
2
I want to convert to string like
JavaScript
1
2
1
"\x31"
2
is there any easy way to do this?
Ultimately I have a byte object like
JavaScript
1
2
1
my_obj = b'x00$x00x00x001.1.0.00x00x00x00x00x00x00x00x001.1.0.21152x00x00'
2
and I want it to have a string with all chars backscaped.
Advertisement
Answer
JavaScript
1
4
1
a = b'12'
2
s = "".join(f"\x{abyte:02x}" for abyte in a)
3
# something like "\x33\x34"
4
I guess … i still think you dont actually want to do this …
if its just for readability then this is a good answer
JavaScript
1
3
1
import binascii
2
print(binascii.hexlify(a," ")) # something like "33 34"
3