C++格式转换集合

1.char*转LPCWSTR

      char m_outSnapFileName[260];

      wchar_t wszbuff[1024] = { 0, };

MByteToWChar(m_outSnapFileName, wszbuff, sizeof(wszbuff) / sizeof(wszbuff[0]));

2.char*转UTF8

static char* char_to_utf8(const char *multiByteStr)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, multiByteStr, -1, NULL, 0);
wchar_t* unicodeStr = NULL;
unicodeStr = (wchar_t *)malloc(len * sizeof(wchar_t));
if (unicodeStr)
{
MultiByteToWideChar(CP_ACP, 0, multiByteStr, -1, unicodeStr, len);
}

len = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, NULL, 0, NULL, NULL);
char* utf8Str = NULL;
utf8Str = (char *)malloc(len);
if (utf8Str)
{
::WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, utf8Str, len, NULL, NULL);
}
char sd[200];
strcpy_s(sd, utf8Str);
free(unicodeStr);
return utf8Str;
}

猜你喜欢

转载自blog.csdn.net/xionglifei2014/article/details/80205657