A
A
amk2016-12-24 03:08:37
Python
amk, 2016-12-24 03:08:37

How to convert string in python from utf-16 to utf-8?

There is a line:

Ą᠄䄄㼄㸄㬄䰄㜄䌄㤄䈄㔀 㨄㸄㐀 㤀 㠀 ㌀㜀㐀 㐄㬄伀 㼄㸄㐄䈄㈄㔄䀄㘄㐄㔄㴄㠄伀 䄄㈄㸄㔄㌄㸀 〄㨄㨄〄䌄㴄䈄
I know that in utf-16be encoding, help me convert to utf-8. I tried using codecs, the result is krakozyabry. The result is in Russian, it has a 6-digit digital code. Simple ways like encode-decode do nothing

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andy_U, 2016-12-25
@ifired

Here is the code:

# -*- coding: utf-8 -*-
s = 'Ą᠄䄄㼄㸄㬄䰄㜄䌄㤄䈄㔀 㨄㸄㐀 㤀 㠀 ㌀㜀㐀 㐄㬄伀 㼄㸄㐄䈄㈄㔄䀄㘄㐄㔄㴄㠄伀 䄄㈄㸄㔄㌄㸀 〄㨄㨄〄䌄㴄䈄'
print(s.encode('utf-16be').decode('utf-16le'))

prints:
Moreover, utf-16be and utf-16le can be interchanged :) In general, it would be better if you brought the original byte string.

A
abcd0x00, 2016-12-24
@abcd0x00

>>> s = 'абвг'.encode('utf-16be')
>>> s
b'\x040\x041\x042\x043'
>>> 
>>> out = s.decode('utf-16be').encode('utf-8')
>>> out
b'\xd0\xb0\xd0\xb1\xd0\xb2\xd0\xb3'
>>> 
>>> out.decode('utf-8')
'абвг'
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question