How to convert python string to ucs2 hex?

I searched for this and could not find it, although it seems simple. I need to send the sixth ucs2 string to url, and I don't know how to convert the python string to hex. Any thoughts?

+3
source share
1 answer
>>> 'åéîøü'.encode('utf16')
b'\xff\xfe\xe5\x00\xe9\x00\xee\x00\xf8\x00\xfc\x00'

(Note that there is a specification at the beginning. Use the encoding 'utf_16_be'or 'utf_16_le'if the end element is fixed.)

If you need hexadecimal digits, use binascii.hexlify.

>>> import binascii
>>> binascii.hexlify('åéîøü'.encode('utf16'))
b'fffee500e900ee00f800fc00'
+5
source

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


All Articles