Python Forum

Full Version: get the content of the byte as string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi

i want to transform

b'p107\xff\xff\xff'
i.e a byte

into

'p107\xff\xff\xff'
i.e a string


how is it done
str(b'p107\xff\xff\xff')
To convert byte strings into string use the decode() method (assuming the byte strings represent unicode strings encoded in some encoding)
>>> bx = b'p107\xff\xff\xff'
>>> sx = bx.decode('latin-1')
>>> sx
'p107ÿÿÿ'
thanks the str() was what i was looking for
if i have

a='x02'

how can i make

b='\x02'

if i do
b="\"+ a
i have an error SyntaxError: EOL while scanning string literal

if i do
b="\\"+a
print(b)
b='\\x02'

which has \\ instead of \

if i do

ord('\x02') results in 2

str='\\'+'x02'
ord(str) gives error
if i have

a='x02'

how can i make

b='\x02'

if i do
b="\"+ a
i have an error SyntaxError: EOL while scanning string literal

if i do
b="\\"+a
print(b)
b='\\x02'

which has \\ insted of \