krishnakumarcn
krishnakumarcn

Reputation: 4179

Cannot convert character array to wstring with utf-8 characters

When I tried to convert char* to wstring using below function in Visual C++.The function is able to convert strings in normal english language, but when I use characters from other language, it is not converting all the characters.

std::wstring s2ws(const char* utf8Bytes)
{
    const std::string& str(utf8Bytes);
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

Example: When I'm printing the converted value into MessageBox, Grüßen is shown as Gr??en

I'm using this converted wstring for obtaining the contents of my directory like below:

map<wstring, wstring> getAllFiles(wstring folder, wstring filter) {  
    wstring directory = folder + L"/" + filter;
    WCHAR szBuf[MAX_PATH];
    WIN32_FIND_DATA d;
    HANDLE hFindFile = FindFirstFile(directory.c_str(), &d);
    .....
}

Here I'm not getting an expected output. i.e., the contents of the directory. But getting it when utf8bytes array is normal English characters.

Upvotes: 0

Views: 223

Answers (2)

Johnny Johansson
Johnny Johansson

Reputation: 270

I think your code is doing the right thing and the problem must be with your UTF-8 string. If I call your code like this, it works as expected:

char utf8buffer[1024];

WideCharToMultiByte(
    CP_UTF8,
    WC_ERR_INVALID_CHARS,
    L"Grüßen",
    -1,
    utf8buffer,
    1024,
    nullptr,
    false
);

assert(s2ws(utf8buffer) == L"Grüßen");

Upvotes: 1

Santosh
Santosh

Reputation: 1815

I think you should change code to below:

std::wstring s2ws(const char* utf8Bytes)
{
    const std::string& str(utf8Bytes);
    int size_needed = MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

Difference between two flags is listed here.

Upvotes: 1

Related Questions