Separating null byte separated UNICODE C string
Question
First off, this is NOT a duplicate of: Turn a C string with NULL bytes into a char array , because the given answer doesn't work when the char *
's are Unicode.
I think the problem is that because I am trying to use UTF-8 encoded char *'s instead of ASCII char *'s, and the length of each character is different and thus, this doesn't work :
char *Buffer; // your null-separated strings
char *Current; // Pointer to the current string
// [...]
for (Current = Buffer; *Current; Current += strlen(Current) + 1)
printf("GetOpenFileName returned: %s\n", Current);
Does anyone have a similar solution that works on Unicode strings?
I have been banging my head on the this for over 4 hours now. C doesn't agree with me.
EDIT: I think that the problem is that the char * is now UTF-8 instead of ASCII.
Solution
Don't use char*
. Use wchar_t*
and the related functions
wchar_t *Buffer; // your null-separated strings
wchar_t *Current; // Pointer to the current string
// [...]
for (Current = Buffer; *Current; Current += wstrlen(Current) + 1)
wprintf(L"GetOpenFileName returned: %s\n", Current);
Incidentally, wchar_t is 16 bits on Windows, not variable-width. If your source data is UTF8-encoded as char*
, you should first convert it to wchar_t*
to work with it.