Some encoding problems

Hi everybody!

I have faced a problem with encoding and wrote this simple example to explain that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define UNICODE
#include <windows.h>

int main()
{
    const char *text = "Русские буковки";

    wchar_t *text_1251 = new wchar_t(strlen(text) + 1);
    memset(text_1251, 0, (strlen(text) + 1) * sizeof(wchar_t));
    MultiByteToWideChar(1251, 0, text, strlen(text), text_1251, strlen(text) + 1);

    wchar_t *text_utf16 = new wchar_t(strlen(text) + 1);
    memset(text_utf16, 0, (strlen(text) + 1) * sizeof(wchar_t));
    MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), text_utf16, strlen(text) + 1);

    MessageBox(NULL, text_1251, L"", MB_OK);
    MessageBox(NULL, text_utf16, L"", MB_OK);

    return 0;
}


When the first message appears, I can read "Русские буковки" (the original text), but the second one only shows black squares. As far as I know, Windows (at least since XP) must support Unicode, but it doesn't work in my example.

Any ideas how to fix it?
Last edited on
Topic archived. No new replies allowed.