如何在C ++中使用UTF-8字符解码URI [英] How to decode an URI with UTF-8 characters in C++

查看:122
本文介绍了如何在C ++中使用UTF-8字符解码URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C ++中解码URI.我发现了一些有关此问题,但它们都无法处理UTF-8编码和重音(我对准确处理ASCII字符很感兴趣).

I need to decode an URI in C++. I found several questions about it, but they all fail to deal with UTF-8 encoding and accents (I'm interested in accurately dealing with ASCII characters).

然后,我使用了一个广泛使用的库,例如libcurl ...,但是它也无法解决UTF-8编码.这就是我在做什么

Then, I went with a broadly used library like libcurl... but it also failed to address the UTF-8 encoding. Here's what I'm doing

string UriHelper::Decode(const string &encoded)
{
    CURL *curl = curl_easy_init();
    int outlength;
    char *cres = curl_easy_unescape(curl, encoded.c_str(), encoded.length(), &outlength);
    string res(cres, cres + outlength);
    curl_free(cres);
    curl_easy_cleanup(curl);
    return res;
}

问题在于,当a%C3%A1e%C3%A9i%C3%ADo%C3%B3u%C3%BA应该为aáeéiíoóuú时,它会被解码为aáeéiíoóuú.如果我使用a%E1e%E9i%EDo%F3u%FA,效果很好.

The problem is that a%C3%A1e%C3%A9i%C3%ADo%C3%B3u%C3%BA gets decoded as aáeéiíoóuú when it should be aáeéiíoóuú. If I use a%E1e%E9i%EDo%F3u%FA it works just fine.

有没有可以处理编码不同的URI并处理它们的库?

Is there any library out there that can take care of differently encoded URIs and deal with them?

谢谢!

推荐答案

您的解码没有问题.问题是打印解码后的URL.打印到的输出设备配置为接受以ISO-8859-1(而非UTF-8)编码的字符串.

There is nothing wrong with your decoding. The printing of the decoded URL is the problem. The output device that you print to is configured to accept strings encoded in ISO-8859-1, not in UTF-8.

将输出设备配置为接受以UTF-8编码的字符串,或者将解码的URL从UTF-8转换为ISO-8859-1.

Either configure the output device to accept strings encoded in UTF-8 or convert the decoded URL from UTF-8 to ISO-8859-1.

这篇关于如何在C ++中使用UTF-8字符解码URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆