C++ c++;字符串UTF-8编码
代码是:C++ c++;字符串UTF-8编码,c++,windows,string,utf-8,C++,Windows,String,Utf 8,代码是: #include <iostream> #include <string> using namespace std; int main() { string test_string = "aáeéöôőüűč♥♦♣♠"; cout << test_string << endl; return 0; } #包括 #包括 使用名称空间std; int main(){ 字符串测试♥♦♣♠"; 不幸的是,在Windo
#include <iostream>
#include <string>
using namespace std;
int main() {
string test_string = "aáeéöôőüűč♥♦♣♠";
cout << test_string << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串测试♥♦♣♠";
不幸的是,在Windows上使用UTF-8是非常有问题的
在Linux上,您可以像这样简单地wstring
:
但不幸的是,Windows没有UTF-8语言环境,所以只能使用Windows API
将文件保存为不带BOM签名的UTF-8,然后尝试使用printf()
//另存为不带BOM签名的UTF8
#包括
#包括
int main(){
设置控制台输出端口CP(65001);
char test_string[]=“aáeőeőuűč♥♦♣♠";
printf(测试字符串);
返回0;
}
结果是:aáeőuűč♥♦♣♠代码>结果不是“错误”而是“意外”"。您需要扩大对环境的期望和了解。对我来说很有用。您使用的是什么操作系统?编译器?源文件编码?如果您想在windows控制台中显示utf-8,祝您好运,请参阅:您必须在运行程序之前更改代码页。我一直在做的事情--这可能是答案,也可能不是答案您正在寻找的是——在工具中将字符串编码为UTF-8,并将其作为转义码逐字节放入,如test\u string=“a\xc3\xa1”“e\xc3\xa9\xc3\xc6\xc3\xc5\x91\xc3\xbc\xc4\xcd\xe2\x99\xa5\xe2\x99\xa3\xe2\x99\xa0”这样,不管源代码文件或任何区域设置的编码,我都确信字符串是正确的UTF-8。你是否限于标准C++,或者你愿意使用Windows API吗?
//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>
int main() {
SetConsoleOutputCP(65001);
char test_string[] = "aáeéöôőüűč♥♦♣♠";
printf(test_string);
return 0;
}