如何从 c++ 或 cocos2dx 中的表情符号中获取 unicode

How to get the unicode from a emoji in c++ or cocos2dx?

本文关键字:符号 获取 unicode c++ cocos2dx      更新时间:2023-10-16

如果我有像" "这样的表情符号,在代码中是:

std::string emojiStr = "  ";

在网站上,表格显示了这个表情符号的 unicode,如"U+1F31E"

请教我如何从 c++/cocos2dx 中的字符串" "中获取这个 unicode?

首先,谢谢你的回答,谢夫!

我在 https://en.cppreference.com/w/cpp/locale/codecvt 中找到了这个例子,并写了一个我所看到的愚蠢的方法。但它毕竟可以工作。

这是代码:

std::string EWUtils::emojiToStr(std::string str)
{
    std::ofstream(FileUtils::getInstance()->fullPathForFilename("text.txt")) << str;
    std::wifstream fin(FileUtils::getInstance()->fullPathForFilename("text.txt"));
    fin.imbue(std::locale("en_US.UTF-8"));
    std::string name;
    for (wchar_t c; fin >> c; )
    {
        __String* item = __String::createWithFormat("%x",c);
        name = name + item->_string + "-";
    }
    if (name.length() > 1) {
        name.pop_back();
    }
    return name;
}

我知道真的没有必要将字符串写入 txt 文件。但如果示例这样写,我就遵循它。

我真的是一个初学者。如果有人有更好的方法,欢迎展示。