N
N
Nahs2015-11-05 14:53:35
Python
Nahs, 2015-11-05 14:53:35

How to convert Cyrillic to ASCII?

I am using Python3.
There is a line
strTxt = 'ной'
I'm trying to convert it to ascii. Something doesn't work.

x = 'ной'.encode(encoding='ascii')
UnicodeEncodeError: 'ascii' codec cant encode characters in position 0-2: ordinal not in range(128)

x = b'ной'
SyntaxError: bytes can only contain ASCII literal characters.

for char in 'ной':
    x=b''
    r = (ord(char) & 0xff)
    x += r.to_bytes(1,byteorder='big')	

print(x) 
b'9'

the output should look like this:
b'\xed\xee\xe9'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2015-11-05
@Nahs

Your cryptic encoding is called cp1251 .
But keep in mind that this relic of the past does not conform to the unicode standard. On modern operating systems, there may be problems with this encoding. For modern portable solutions it's better to use utf8 as pygame said .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question