简体   繁体   中英

How to convert string from UCS2 to readable text

I need to convert the given string from UCS2 to readable text. How can I implement this in Python and C++ Arduino without using third-party modules.

st ="041204410451002004210443043F04350440003A00200031003300200413041100200438043D044204350440043D043504420430002C00200031003500300020043C0438043D00200030002004410435043A0020043D04300020043C043E04310438043B044C043D044B043500200420041A00200434043E002000320037002E00300038002E00320032"

I found this code, but it does not work as it should. Can you please tell me how to do the correct calculation?

def con():
    UCS2ToChar = ''
    res = ""
    arrUCS2 = list("0412")

    if (arrUCS2[1] == '4'):
        if (arrUCS2[2] == '0'): UCS2ToChar = 89
        elif (arrUCS2[2] == '1'): UCS2ToChar = 64
        elif (arrUCS2[2] == '2'): UCS2ToChar = 48
        elif (arrUCS2[2] == '3'): UCS2ToChar = 32
        elif (arrUCS2[2] == '4'): UCS2ToChar = 16
        elif (arrUCS2[2] == '4'): UCS2ToChar = 73

        if (int(arrUCS2[3]) > int('9')):
            UCS2ToChar -= (int(arrUCS2[3]) - 55)
        else:
            UCS2ToChar -= (int(arrUCS2[3]) - int('0'))
        UCS2ToChar = (int(UCS2ToChar))
        res += (chr(UCS2ToChar))
        print(res)
con()

If you do this print (ord ('B')) then the code of the letter (which, in theory, is encrypted there) will be different from that obtained using this enumeration.

Based on Python UCS2 decoding from hex string - Stack Overflow

import binascii

st = "041204410451002004210443043F04350440003A00200031003300200413041100200438043D044204350440043D043504420430002C00200031003500300020043C0438043D00200030002004410435043A0020043D04300020043C043E04310438043B044C043D044B043500200420041A00200434043E002000320037002E00300038002E00320032"

text = binascii.unhexlify(st).decode('utf-16-be')

print(text)

or

import codecs

st = "041204410451002004210443043F04350440003A00200031003300200413041100200438043D044204350440043D043504420430002C00200031003500300020043C0438043D00200030002004410435043A0020043D04300020043C043E04310438043B044C043D044B043500200420041A00200434043E002000320037002E00300038002E00320032"

text = codecs.decode(st, 'hex').decode('utf-16-be') 

print(text)

Result:

Всё Супер: 13 ГБ интернета, 150 мин 0 сек на мобильные РК до 27.08.22

binascii and codecs are standard modules preinstalled with Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM