我正在尝试从unicode获取utf8代码。现在编写,我有代码可以将任何INT转换为unicode,但不得不继续到UTF8。有什么想法吗?
发布于 2016-10-11 09:07:12
Windows中的函数WideCharToMultiByte将为您正确地执行此操作。这是WideCharToMultiByte参考页。
要使用WideCharToMultiByte,您需要选择一个代码页。常见的代码页是CP_ACP和CP_UTF8。CP_ACP用于在ANSI和Unicode之间进行转换。CP_UTF8用于UTF-8和Unicode之间的转换.
以下是一个示例:
string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}https://stackoverflow.com/questions/33067266
复制相似问题