如何将python字符串转换为ucs2 hex? [英] How do I convert a python string to ucs2 hex?

查看:94
本文介绍了如何将python字符串转换为ucs2 hex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管看起来很简单,但我一直在搜索它,但找不到.我需要在网址中发送一个ucs2十六进制字符串,而且我不知道如何将python字符串转换为ucs2十六进制.有什么想法吗?

I've been searching for this one and couldn't find it, although it seems 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'

(请注意,开头是BOM表.如果尾数是固定的,请使用编码'utf_16_be''utf_16_le'.)

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

如果您需要十六进制数字,请使用 binascii.hexlify .

If you need hex digits, use binascii.hexlify.

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

这篇关于如何将python字符串转换为ucs2 hex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆