开发者

Convert unicode string to array of bytes

开发者 https://www.devze.com 2022-12-20 10:48 出处:网络
I\'m using OpenGL and I 开发者_StackOverflow中文版need to pass to a function array of bytes. glCallLists(len(\'text\'), GL_UNSIGNED_BYTES, \'text\');

I'm using OpenGL and I 开发者_StackOverflow中文版need to pass to a function array of bytes.

glCallLists(len('text'), GL_UNSIGNED_BYTES, 'text');

This way it's working fine. But I need to pass unicode text. I think that it should work like this:

text = u'unicode text'
glCallLists(len(text), GL_UNSIGNED_SHORT, convert_to_array_of_words(text));

Here I use GL_UNSIGNED_SHORT that says I'll give array where each element takes 2 bytes, and somehow convert unicode text to array of words.

So, how can I convert unicode string to "raw" array of chars' numbers?


The UTF encoding that takes up 2 bytes per character is UTF-16:

print repr(u'あいうえお'.encode('utf-16be'))
print repr(u'あいうえお'.encode('utf-16le'))
0

精彩评论

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

关注公众号