繁体   English   中英

为什么字符变得无用? libcurl c ++ Utf-8编码的html;

[英]Why chars become useless? libcurl c++ Utf-8 encoded html;

首先抱歉我的英语不好。 我做了我的研究,但没有任何相关的答案来解决我的问题。 我已经理解并了解了CodePages Utf 8以及c或c ++中的其他内容,并且知道字符串可以容纳utf8。 我的开发机器winxp英语,控制台代码页设置为1254(windows turkish),我可以在std :: string中使用土耳其语扩展字符(İığşçüö),计算它们并将它们发送到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。

另外,您是否检查过设置代码页是否成功? SetConsoleOutputCP函数通过其返回值指示成功或失败。

返回的字符串是utf-8,因此您应该将控制台代码页设置为65001(由sth推荐)。 或者将字符串转换为1254并使用1254代码页进行控制台输出,就像之前一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM