开发者

How do I convert a python string to ucs2 hex?

开发者 https://www.devze.com 2023-01-08 09:13 出处:网络
I\'ve been searching for this one and couldn\'t find it, although it se开发者_高级运维ems simple. I need to send in a ucs2 hex string in the url, and I don\'t know how to convert a python string to be

I've been searching for this one and couldn't find it, although it se开发者_高级运维ems simple. I need to send in a ucs2 hex string in the url, and I don't know how to convert a python string to be ucs2 hex. Any thoughts?


>>> 'åéîøü'.encode('utf16')
b'\xff\xfe\xe5\x00\xe9\x00\xee\x00\xf8\x00\xfc\x00'

(Note that there's a BOM in the beginning. Use the encoding 'utf_16_be' or 'utf_16_le' if the endian is fixed.)

If you need hex digits, use binascii.hexlify.

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

精彩评论

暂无评论...
验证码 换一张
取 消