首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在可视化utf8中将unicode转换为c++?

如何在可视化utf8中将unicode转换为c++?
EN

Stack Overflow用户
提问于 2015-10-11 16:20:08
回答 1查看 547关注 0票数 1

我正在尝试从unicode获取utf8代码。现在编写,我有代码可以将任何INT转换为unicode,但不得不继续到UTF8。有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-10-11 09:07:12

Windows中的函数WideCharToMultiByte将为您正确地执行此操作。这是WideCharToMultiByte参考页

要使用WideCharToMultiByte,您需要选择一个代码页。常见的代码页是CP_ACP和CP_UTF8。CP_ACP用于在ANSI和Unicode之间进行转换。CP_UTF8用于UTF-8和Unicode之间的转换.

以下是一个示例:

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33067266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档