Python & # 8594; replace escape character to convert hex string from 0x to \ x

I am trying to print ascii or an extended ascii character. using this code:

print '\xff'.decode('latin-1')

it will print ascii # 255, but now I want to enter using a decimal number as follows:

num=255
myhex=hex(num) 
print myhex.decode('latin-1')

Coz myhex is '0xff' does not work, so I need to convert to '\ xff'. replacing "0x" with "\ x" gives me an error.

myhex.replace('0x','\x') 

will give me an error: ValueError: invalid \ x escape

How to solve the problem? can anyone help? the target I want to print char β†’ ΓΏ in the terminal / console.

+3
source share
2 answers

what you are looking for

chr(255)

python2, 255. , UTF8, ? ( ), , . unicode, decode :

chr(255).decode('latin1')

python3, chr(255) charicer unicode 'LATIN SMALL LETTER Y WITH DIAERESIS'.

python2,

unichr(255)
+3
>>> print chr(255).decode("latin-1")
ΓΏ
+1

All Articles