有没有一种在Visual Studio中编写UTF-8八位字节的简单方法?

Mic*_*dej 7 c++ encoding utf-8 visual-studio

我有一个问题,我需要在C++源代码中使用标准字符类型的UTF-8编码字符串,如下所示:

char* twochars = "\xe6\x97\xa5\xd1\x88";
Run Code Online (Sandbox Code Playgroud)

通常,如果我想写一个UTF-8字符,我需要使用上面的八位字节.Visual Studio中有什么东西(我使用VS 2013 Ultimate)可以让我只编写例如"ĄĘĆŻ"并自动将每个字符转换为多个UTF-8八位字节,如上例所示?或者我应该使用const wchar_t*并找到一个可以将宽字符串转换为UTF-8编码的标准字符串字符串的库?

如果没有这样的东西,你能为此建议任何外部软件吗?我真的不想浏览每个符号/非拉丁字母的字符映射.

对不起我的英文,提前致谢.

Jig*_*ore 16

您可以使用仍未记录的 pragma指令execution_character_set("utf-8").这样,您的char字符串将在您的二进制文件中保存为UTF-8.顺便说一句,这个pragma仅在Visual C++编译器中可用.

#include <iostream>
#include <cstring>

#pragma execution_character_set("utf-8")

using namespace std;

char *five_chars = "????!";

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "This is an UTF-8 string: " << five_chars << endl;
    cout << "...it's 5 characters long" << endl;
    cout << "...but it's " << strlen(five_chars) << " bytes long" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)