WinRT(C++/CX) UTF8类型转换为std::string和Platform::String^的Unicode字符串

Gist 仓库地址:https://gist.github.com/huihut/8f75e2332e05673ff7e1248ad5e85339

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <Windows.h>

std::string UTF8_To_Std_Str(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

wchar_t* pwBuf = new wchar_t[nwLen + 1];
memset(pwBuf, 0, nwLen * 2 + 2);

MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

char* pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);

WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string retStr = pBuf;

delete[] pBuf;
delete[] pwBuf;

pBuf = NULL;
pwBuf = NULL;

return retStr;
}

Platform::String^ UTF8_To_Managed_Str(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

wchar_t* pwBuf = new wchar_t[nwLen + 1];
memset(pwBuf, 0, nwLen * 2 + 2);

MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

Platform::String^ pStr = ref new Platform::String(pwBuf);

delete[] pwBuf;
pwBuf = NULL;

return pStr;
}
修电脑的裁缝酱 wechat
扫描微信公众号关注我