我希望能将一个简单的Unicode字符串转换为字节数组,以便我可以将每个字符引用为int。这是否可行?
我想将一个字符串
我该如何高效地完成这个任务?
我想将一个字符串
u"Hello World"
转换成UTF-8编码,并得到类似于以下格式的结果:`[0x01, 0x02, ..., 0x02]
我该如何高效地完成这个任务?
如果你正在寻找Python的bytearray:
my_array = bytearray(u"hello, world", encoding="utf-8")
output = []; for i in ba: output.add(hex(i)) ;; print ", ".join(output)
。为胜利而战! - Naftuli Kayprint ", ".join(map(hex, ba))
。 - David Robinsonreturn "".join(map(hex, bytearray(in_string, encoding="utf-8"))).replace('0x','')
- Nick Sweetinglist_of_bytes = map(ord, my_unicode_string.encode('utf8'))
list_of_code_points = map(ord, my_unicode_string)
[0..255]
范围内的代码点,前者似乎更有可能。 - user395760u'誠'
,您将得到[232, 170, 160]
,而后者将为您提供完整的Unicode点值(8AA0 -> 35488
)。根据@TK Kocheran想要实现的目标,这两种方法都可以很好地工作。 - Makotohex()
映射到每个值,以便我在数组中拥有它们的十六进制字符串? - Naftuli Kaylambda x: hex(ord(x))
代替ord
。 - dhwthompson