A
A
Alexander Kunin2017-03-26 13:25:04
ruby
Alexander Kunin, 2017-03-26 13:25:04

How to convert characters from UTF-8 to \320\220 format?

I'm trying to solve a string encoding puzzle.
Required from line:
Administrator
Get:
\320\220\320\264\320\274\320\270\320\275\320\270\321\201\321\202\321\200\320\260\321\ 202\320\276\321\200
i.e.
A = \320\220
d = \320\264
m = \320\274
u = \320\270
n = \320\275
u = \320\270
c = \321\201
t = \321\202
p = \321\200
a = \320\260
m = \321\202
o = \320\276 p = \ 321
\200
Source string in UTF-8

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-03-26
@skyksandr

print ''.join(['\\'+oct(ord(c))[1:] for c in u'Администратор'.encode('utf-8')])

320 is the octal notation 0xD0 and 0xD0 is the first byte of the character in UTF-8
so get the bytes encoded in UTF-8 .encode('utf-8') then convert the byte to its decimal notation ord and the decimal notation to octal oct . since in octal from '\xD0' we get '0320' - then we cast it to '320' [1:] as a result, we create an array of elements starting with \ '\\'+ , and in the final we join the array into the string ''.join () .
I just took it and decided, I had not met before:
94ee110bb6ba4059b48d9a2404242782.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question