user1474685
user1474685

Reputation:

Null character in unicode string

I know the code in C is this:

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

So I tried (in Python):

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

But this strips the Null character of or something. If I use create_string_buffer it does work.

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

Now the the string has 2 Null characters at the end of it:

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

So why does this not work for unicode?

Edit:

This works (source: Python Ctypes Null Terminated String Block):

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

Upvotes: 2

Views: 7358

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177901

This works nicely. Note the single-parameter init of create_unicode_buffer nul-terminates:

>>> r=ctypes.create_unicode_buffer('String\0Other')
>>> r.value              # value display stops at first nul.
u'String'
>>> ''.join(r)           # gives answer similar to create_string_buffer's raw method.
u'String\x00Other\x00'

Upvotes: 1

Related Questions