我的VS2015工程使用的是unicode编码,插入中文显示一堆问号,解决办法:
unicode编码转成UTF8
static std::wstring UniAsciiToUnicode(const std::string& str)
{
// 预算-缓冲区中宽字节的长度
int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
// 给指向缓冲区的指针变量分配内存
wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
// 开始向缓冲区转换字节
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
std::wstring ret_str = pUnicode;
free(pUnicode);
return ret_str;
}
static std::string UnicodeToUtf8(const std::wstring& wstr)
{
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
// 给指向缓冲区的指针变量分配内存
char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, NULL, NULL);
std::string ret_str = pAssii;
free(pAssii);
return ret_str;
}
//ANSI转UTF8 最终转换函数
static std::string AsciiToUtf8(const std::string& str)
{
return UnicodeToUtf8(UniAsciiToUnicode(str));
}