LPCWSTR, string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int main()
{
    LPCWSTR s=L"йаᠭ1";
    char str[8];
    size_t sizeRequired;
    ZeroMemory(str, 8);
    sizeRequired = WideCharToMultiByte( CP_UTF8, 0, s, 1, str, 4,  NULL, NULL);
    std::cout << "The letter : " << str << endl;
    std::cout <<"The size: "<< sizeRequired << endl;
return 0;
}


The output is:
The letter : й
The size : 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
wstring s2ws(const std::string& s)
{
	 int len;
	 int slength = (int)s.length() + 1;
	 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
	 wchar_t* buf = new wchar_t[len];
	 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
	 std::wstring r(buf);
	 delete[] buf;
	 return r;
}
int main()
{
    string str="йаᠭ1";
    wstring wstr=s2ws(str.c_str());
    LPCWSTR s=wstr.c_str();
    char str[8];
    size_t sizeRequired;
    ZeroMemory(str, 8);
    sizeRequired = WideCharToMultiByte( CP_UTF8, 0, s, 1, str, 4,  NULL, NULL);
    std::cout << "The letter : " << str << endl;
    std::cout <<"The size: "<< sizeRequired << endl;
return 0;
}


The output is:
The letter : Ñ
The size : 3

Why outputs are different here? On the second block code I just converted string to LPCWSTR'. Did I converted wrong?
Topic archived. No new replies allowed.