问题描述
我有一个问题,我需要使用UTF-8编码的字符串在标准的char类型在C ++源代码如下:
I have a problem, I need to use UTF-8 encoded strings on standard char types in C++ source code like so:
char* twochars = "\xe6\x97\xa5\xd1\x88";
通常,如果我想写一个UTF-8字符,我需要使用上面的八位字节。在Visual Studio中有什么东西(我使用的是VS 2013 Ultimate),这可能允许我只写一个例如ĄĘĆŻ,并自动将每个字符转换为多个UTF-8八位字节,如上例所示?或者应该使用 const wchar_t *
并找到一个可以将宽字符串转换为UTF-8编码的标准字符串的库?
Normally, if I want to write an UTF-8 character I need to use octets like above. Is there something in Visual Studio (I'm using VS 2013 Ultimate) that could allow me to just write for example "ĄĘĆŻ" and automagically converted each character to multiple UTF-8 octets like in the example above? Or should I use const wchar_t*
and find a lib that could convert wide strings to UTF-8 encoded standard char strings?
如果没有这样的事情,你能建议任何外部软件吗?我真的不想浏览每个符号/非拉丁字母的字符映射。
If there is no such thing, could you suggest any external software for that? I really don't feel like browsing the character map for every symbol/non-latin letter.
对不起,我的英语,
提前感谢。 p>
Sorry for my English,Thanks in advance.
推荐答案
您可以使用 pragma伪指令 execution_character_set(utf-8)
。这样,您的 char
字符串将以二进制形式保存为UTF-8。 BTW,此编译指示仅适用于Visual C ++编译器。
You can use the still undocumented pragma directive execution_character_set("utf-8")
. This way your char
strings will be saved as UTF-8 in your binary. BTW, this pragma is available in Visual C++ compilers only.
#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;
}
这篇关于在Visual Studio中有一个简单的方法来写UTF-8字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!