Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将unicode字符串转换为字节数组_Python_Unicode - Fatal编程技术网

Python 将unicode字符串转换为字节数组

Python 将unicode字符串转换为字节数组,python,unicode,Python,Unicode,我正在使用OpenGL,我需要传递一个字节的函数数组 glCallLists(len('text'), GL_UNSIGNED_BYTES, 'text'); 这样很好用。但我需要传递unicode文本。我认为应该是这样的: text = u'unicode text' glCallLists(len(text), GL_UNSIGNED_SHORT, convert_to_array_of_words(text)); 这里我使用了GL\u UNSIGNED\u SHORT,它说我将给出一个

我正在使用OpenGL,我需要传递一个字节的函数数组

glCallLists(len('text'), GL_UNSIGNED_BYTES, 'text');
这样很好用。但我需要传递unicode文本。我认为应该是这样的:

text = u'unicode text'
glCallLists(len(text), GL_UNSIGNED_SHORT, convert_to_array_of_words(text));
这里我使用了
GL\u UNSIGNED\u SHORT
,它说我将给出一个数组,其中每个元素取2个字节,并以某种方式将unicode文本转换为单词数组


那么,如何将unicode字符串转换为字符数字的“原始”数组呢?

每个字符占用2个字节的UTF编码是UTF-16:

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

我认为没有必要进行任何转换。Unicode文本应该已经是一个无符号数组shorts@John:取决于库是使用UCS-2还是UCS-4生成的。可能是,但我在尝试传递unicode字符串时遇到此错误:
ctypes.ArgumentError:参数3::没有类型的数组类型处理程序(值:u'\u0439')registered
@Ignacio:他的代码中的字符串文字是如何成为库问题的?你是说OpenGL吗?library@John:不,我是说Python库。是的,它们可以。但是,我说它每个字符使用2个字节是不准确的。有些将占用4个字节,由“代理项对”组成。@Mike:我想你的意思是不是所有的代码点都可以在UCS-2中表示。