unicode字符串中的空字符

2024-10-04 01:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道C中的代码是这样的:

WCHAR string[] = L"string\0";

所以我尝试(在Python中):

string = ctypes.create_unicode_buffer("String\0")

但这会去掉的空字符。 如果我使用create_string_buffer它确实有效。

string = ctypes.create_string_buffer("String\0")

现在字符串的末尾有两个空字符:

>>> string.raw
'String\x00\x00'

为什么这对unicode不起作用?

编辑:

这是可行的(来源:Python Ctypes Null Terminated String Block):

string = ctypes.wstring_at(ctypes.create_unicode_buffer(u'string\0'), 7)

Tags: 字符串代码编辑stringrawbuffercreate来源