【发布时间】:2011-11-27 15:25:59
【问题描述】:
首先对不起我的英语不好。 我已经完成了我的研究,但没有任何相关的答案可以解决我的问题。 我已经了解并了解了 CodePages Utf 8 和其他关于 c 或 c++ 的内容, 并且还知道字符串可以保存 utf8。 我的开发机器 winxp 英语,控制台代码页设置为 1254(Windows 土耳其语),我可以在 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 'ı','ö' 打印出 'Ķ','ğ' 打印出 'ÄŸ','İ' 打印出 'Ä˚' 等等。
如果我将 CodePage 更改为 65000,
...
SetConsoleOutputCP(65000);//For utf8
...
那么结果是一样的,所以问题的原因不是cmd CodePage。
响应http headers表示charset设置为utf-8和html元数据是一样的。
据我了解,问题的根源在于函数“writer”或“curl”本身。传入的数据被解析为字符,因此像 ı,İ,ğ 这样的扩展字符被解析为 2 个字符并写入 char 数组 std::string 以这种方式因此代码页相当于这些半字符打印出来或在代码中的任何位置使用(例如 mysqlpp 编写该字符串到 db)。
我不知道如何解决这个问题,也不知道在 writer 函数或其他任何地方该做什么。 我想对了吗?如果是这样,我该怎么办?还是问题的根源在别处?
我正在使用 mingw32 Windows Xp 32bit Code::Blocks ide。
【问题讨论】:
-
欢迎来到stackoverflow!不要担心你的英语,你在这里度过的时间会变得更好。我知道我的:)
-
抱歉跑题了,但它是什么语言(我的意思是ğşçöüİı)?
-
ğçşçöüİı 是土耳其语中不同于英语的特殊字母,而且土耳其语字母表中不存在字母 xwq。
标签: c++ string utf-8 libcurl codepages