为什么字符变得无用? libcurl c Utf-8编码的html;
首先抱歉我的英语不好.
我做了我的研究,但没有任何相关的答案来解决我的问题. 我已经了解并了解了CodePages Utf 8以及c或c中的其他内容, 并且知道字符串可以容纳utf8. 我的开发机器winxp英语,控制台代码页设置为1254( windows土耳其语),我可以在std :: string中使用turkish扩展字符(?????ü?),计算它们并将它们发送到 mysqlpp api来写入dbs.没有问题.但是,当我想使用curl获取一些html并将其写入std :: string时,我的问题就开始了. #include <iostream> #include <windows.h> #include <wincon.h> #include <curl.h> #include <string> int main() { SetConsoleCP(1254); SetConsoleOutputCP(1254); std::string s; std::cin>>s; std::cout<<s<<std::endl; return 0; } 当我运行这些并键入????ü??输出是相同的????ü??; #include <iostream> #include <windows.h> #include <wincon.h> #include <curl.h> #include <string.h> size_t writer(char *data,size_t size,size_t nmemb,std::string *buffer); { int res; if(buffer!=NULL) { buffer->append(data,size*nmemb); res=size*nmemb; } return res; } int main() { SetConsoleOutputCP(1254); std::string html; CURL *curl; CURLcode result; curl=curl_easy_init(); if(curl) { curl_easy_setopt(curl,CURLOPT_URL,"http://site.com"); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer); curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html); result=curl_easy_perform(curl); if(result==CURLE_OK) { std::cout<<html<<std::endl; } } return 0; } 当我编译并运行; 如果html包含’?’打印到cmd’?±’,’?’打印’??’,’?’pirntsout’??’,’?’打印出’??’等. 如果我将CodePage更改为65000, ... SetConsoleOutputCP(65000);//For utf8 ... 那么结果是一样的,所以问题的原因不是cmd CodePage. 响应的http标头表示charset设置为utf-8,html元数据是相同的. 据我所知,问题的根源是“作家”或“卷曲”本身的功能.传入的数据解析为chars所以扩展的字符如?,?,?解析为2个字符并用这种方式写入char数组std :: string因此代码页相当于这些半字符打印出来或在代码中的任何地方使用(例如mysqlpp写入那个字符串到db). 我不知道如何在编写器功能或其他任何地方解决这个问题或做什么. 我使用mingw32 Windows Xp 32位Code :: Blocks ide. 解决方法
UTF-8的正确代码页是
65001,而不是65000.
另外,您是否检查过设置代码页是否成功? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |