uoa*_*nci 5 c++ string utf-8 libcurl codepages
首先,抱歉我的英语不好。\n我已经完成了研究,但没有任何相关答案来解决我的问题。\n我已经理解并了解了 CodePages Utf 8 以及有关 c 或 c++ 中的其他内容,\nand还知道字符串可以保存 utf8。\n我的开发机器 winxp english,控制台代码页设置为 1254(Windows 土耳其语),我可以使用土耳其语扩展字符 (\xc4\xb0\xc4\xb1\xc4\x9f\xc5\x9f\xc3 \xa7\xc3\xbc\xc3\xb6) 在 std::string 中,计算它们并将它们发送到 mysqlpp api 以写入数据库。没有问题。但是当我想使用curl 获取一些html 并将其写入std::string 时,我的问题就开始了。
\n\n#include <iostream>\n#include <windows.h>\n#include <wincon.h>\n#include <curl.h>\n#include <string>\nint main()\n{\n SetConsoleCP(1254);\n SetConsoleOutputCP(1254);\n std::string s;\n std::cin>>s;\n std::cout<<s<<std::endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我运行这些并输入 \xc4\x9f\xc5\x9f\xc3\xa7\xc3\xb6\xc3\xbc\xc4\xb0\xc4\xb1 时,输出是相同的 \xc4\x9f\xc5\x9f\xc3\ xa7\xc3\xb6\xc3\xbc\xc4\xb0\xc4\xb1;
\n\n#include <iostream>\n#include <windows.h>\n#include <wincon.h>\n#include <curl.h>\n#include <string.h>\n\nsize_t writer(char *data, size_t size, size_t nmemb, std::string *buffer);\n{\n int res;\n if(buffer!=NULL)\n {\n buffer->append(data,size*nmemb);\n res=size*nmemb;\n }\n return res;\n}\nint main()\n{\n SetConsoleOutputCP(1254);\n std::string html;\n CURL *curl;\n CURLcode result;\n curl=curl_easy_init();\n if(curl)\n {\n curl_easy_setopt(curl, CURLOPT_URL, "http://site.com");\n curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);\n curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);\n result=curl_easy_perform(curl);\n if(result==CURLE_OK)\n {\n std::cout<<html<<std::endl;\n }\n }\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我编译并运行时;
\n\n如果html包含\'\xc4\xb1\'打印到cmd \'\xc3\x84\xc2\xb1\',\'\xc3\xb6\'打印出\'\xc3\x84\xc2\xb6\ ', \'\xc4\x9f\' 打印出 \'\xc3\x84\xc5\xb8\', \'\xc4\xb0\' 打印出 \'\xc3\x84\xcb\x9a\' 等。
\n\n如果我将代码页更改为 65000,
\n\n...\nSetConsoleOutputCP(65000);//For utf8\n...\nRun Code Online (Sandbox Code Playgroud)\n\n那么结果是相同的,因此问题的原因不是 cmd CodePage。
\n\n响应http标头表明字符集设置为utf-8并且html元数据是相同的。
\n\n据我了解,问题的根源是函数“writer”或“curl”本身。传入数据解析为字符,因此扩展字符如 \xc4\xb1、\xc4\xb0、\xc4\x9f 解析为 2 个字符并以这种方式写入字符数组 std::string ,因此代码页相当于这些半字符打印或使用代码中的任何位置(例如 mysqlpp 将该字符串写入数据库)。
\n\n我不知道如何解决这个问题,也不知道在编写器功能或其他任何地方要做什么。\n我的想法正确吗?如果是这样我该怎么办这个问题?或者问题的根源在其他地方?
\n\n我使用 mingw32 Windows Xp 32 位 Code::Blocks ide。
\n