现象
将中文字符串赋值给string类型变量, 并将该变量序列化,通过websocket发送给前端。
SetConsoleOutputCP(65001);
std::cout << "console encode type : 65001" << std::endl;
std::cout << "cpp file encode type : 936" << std::endl;
std::string info = "中文字符串_test";
std::cout << info << std::endl;
此时控制台输出该变量为乱码:
![](https://i-blog.csdnimg.cn/blog_migrate/da501f40945b5138b3cae0c50572d6ea.png)
且前端无法以UTF-8格式解析字符串,导致websocket断开连接:
![](https://i-blog.csdnimg.cn/blog_migrate/e0d0a393ba86efa0b224487bdc338d4d.png)
解决方法
通过编译器选项指定执行字符集
点击“项目--项目属性”打开属性页窗口,点击“配置属性--C/C++--命令行”,在“其他选项”中填写“/execution-c