UTF-8 latin-1 conversion issues, python django

ok, so my problem is that I have the string '\ 222 \ 222 \ 223 \ 225' which is stored as latin-1 in db. What I get from django (by printing it) is the following line: "ââââ" which I assume is a UTF conversion. Now I need to pass a string to a function that performs this operation:

strdecryptedPassword + chr(ord(c) - 3 - intCounter - 30)

I get this error:

chr () arg is not in the range (256)

If I try to encode the string as latin-1 first, I will get this error:

'latin-1' codec cannot encode characters at position 0-3: serial number in range (256)

I read a bunch about how character encoding works, and something is missing for me because I just don't understand!

+3
source share
3

"chr() arg (256)", , , , chr . , , inputcounter + 33 , , , .

. (), () , . encode() unicode (, u), , . decode() unicode . unicode() , a.decode('latin-1').

>>> a = '\222\222\223\225'
>>> u = unicode(a,'latin-1')
>>> u
u'\x92\x92\x93\x95'
>>> print u.encode('utf-8')
ÂÂÂÂ
>>> print u.encode('utf-16')
ÿþ
>>> print u.encode('latin-1')

>>> for c in u:
...   print chr(ord(c) - 3 - 0 -30)
...
q
q
r
t
>>> for c in u:
...   print chr(ord(c) - 3 -200 -30)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: chr() arg not in range(256)
+4

Vinko, Latin-1 ISO 8859-1 , . 8859-1, "C1 Controls (0x80 - 0x9F) ISO/IEC 6429: 1992. 80, 81 99". , Vinko, :

\222 = 0x92 => PRIVATE USE TWO
\223 = 0x93 => SET TRANSMIT STATE
\225 = 0x95 => MESSAGE WAITING

UTF-8 (Unicode, , ):

U+0092 = %11000010 %10010010 = 0xC2 0x92
U+0093 = %11000010 %10010011 = 0xC2 0x93
U+0095 = %11000010 %10010101 = 0xC2 0x95

LATIN SMALL LETTER A CIRCUMFLEX - ISO 8859-1 0xE2 , , Unicode U + 00E2; UTF-8, % 11000011% 10100010 0xC3 0xA2.

CENT SIGN - ISO 8859-1 0xA2 , , Unicode U + 00A2; UTF-8, % 11000011% 10000010 0xC3 0x82.

, , , , UTF-8 ISO 8859-1. , , 5 , 8.

: UTF-8, , :

Now I need to pass the string into a function that does this operation:

    strdecryptedPassword + chr(ord(c) - 3 - intCounter - 30)

I get this error: chr() arg not in range(256).  If I try to encode the
string as Latin-1 first I get this error: 'latin-1' codec can't encode
characters in position 0-3: ordinal not in range(256).

, intCounter, , 'ord(c) - 3 - intCounter - 30' (, , 'ord(c) - intCounter - 33'?), , chr(), , . 256, , , 0 255, chr(). , , , 0 255 . , , :

chr(mod(ord(c) - mod(intCounter, 255) + 479, 255))

256 - 33 = 223, , 479 = 256 + 223. , , chr(), 0..255 c intCounter (, mod() , , mod() , ).

+2

, - , ord() , , , , . , , , . latin-1, django unicode, unicode, latin-1, .

0

Source: https://habr.com/ru/post/1699311/


All Articles