C++ 将URL编码转换为可打印字符

C++ 将URL编码转换为可打印字符,c++,c++11,uri,url-encoding,urldecode,C++,C++11,Uri,Url Encoding,Urldecode,我必须处理包含URL编码的字符串,如“%C3%A7”,我需要将这些序列转换为相应的可打印字符。因此我写了一个函数。这是可行的,但似乎相当尴尬。我是一个绝对的C/C++初学者。也许有人能给我指出一个更优雅的解决方案 #include <iostream> using namespace std; static inline void substitute_specials(string &str) { const struct {string from,to;}

我必须处理包含URL编码的字符串,如“%C3%A7”,我需要将这些序列转换为相应的可打印字符。因此我写了一个函数。这是可行的,但似乎相当尴尬。我是一个绝对的C/C++初学者。也许有人能给我指出一个更优雅的解决方案

#include <iostream> 

using namespace std;

static inline void substitute_specials(string &str) {
    const struct {string from,to;} substitutions[] { { "20"," " },{ "24","$" },{ "40","@" },{ "26","&" },{ "2C","," },{ "C3%A1","á" },{ "C3%A7","ç" },{ "C3%A9","é" } };
    size_t start_pos = 0;
    while ((start_pos = str.find("%", start_pos)) != string::npos) {
        start_pos++;
        for (int i=0; i< extent < decltype(substitutions) > ::value; i++) {
            if (str.compare(start_pos,substitutions[i].from.length(),substitutions[i].from)  == 0) {
                    str.replace(start_pos-1, substitutions[i].from.length()+1, substitutions[i].to);
                    start_pos += substitutions[i].to.length()-1;
                break; 
            }
        }
    }
}

int main() {
    string testString = "This%20is %C3%A1 test %24tring %C5ith %40 lot of spe%C3%A7ial%20charact%C3%A9rs%2C %26 worth many %24%24%24";
    substitute_specials(testString);
    cout << testString << "\n";
    return 0;
}
#包括
使用名称空间std;
静态内联void替换_特殊项(字符串和字符串){
const struct{string from,to;}替换[]{{“20”,“24”,“24”,“$”},{“40”,“@”},{“26”,“和”},{“2C”,“,”},{“C3%A1”,“á”},{“C3%A7”,“ç”},{“C3%A9”,“é”};
大小\u t开始位置\u位置=0;
while((start_pos=str.find(“%,start_pos))!=string::npos){
启动_pos++;
对于(int i=0;i::value;i++){
if(str.compare(开始位置,替换[i].from.length(),替换[i].from)==0){
str.replace(start_pos-1,替换[i].from.length()+1,替换[i].to);
start_pos+=替换[i].to.length()-1;
打破
}
}
}
}
int main(){
string testString=“这%20是%C3%A1测试%24字符串%c5和%40批spe%C3%A7ial%20character%C3%A9rs%2C%26,价值很多%24%24%24”;
替换_特殊项(测试字符串);

我自己问题的答案是这个unescape/undecode URI例程,它也处理2和3字节序列:

我自己问题的答案是这个unescape/undecode URI例程,它也处理2和3字节序列:

这些不是UTF8“编码”。它们是URL(?)转义序列。您在此页面中看到的是UTF8字符。在UTF8中,ASCII字符显示相同,非ASCII字符使用2个或更多字节存储,但显示为一个字符。您需要URL解码方法。顺便说一句,您需要
u8
前缀,例如
u8“Δx=%”或直接到字符串
自动测试字符串=u8“Δx=%”解释了如何在C++中使用UTF8、UTF16等,以非常好的方式解释了如何使用UTF8、UTF16等。从潜在的无穷大中解码8个序列?这些可能的副本不是UTF8“编码”。它们是URL(?)转义序列。您在此页面中看到的是UTF8字符。在UTF8中,ASCII字符显示相同,非ASCII字符使用2个或更多字节存储,但显示为一个字符。您需要URL解码方法。顺便说一句,您需要
u8
前缀,例如
u8“Δx=%”或直接到字符串
自动测试字符串=u8“Δx=%”解释了如何在C++中使用UTF8、UTF16等,非常好的方法。你想做什么?从潜在无穷大中解码8个序列?可能的副本
wstring url_decode2(char* SRC) {

wstring ret;
wchar_t ch;
int i, ii;
char sub[5];

for (i=0; i<strlen(SRC); i++) {
    if (SRC[i]=='%') {
        if ((SRC[i+3]=='%') && (SRC[i+1]>='A')) {
            sub[0]=SRC[i+4]; 
            sub[1]=SRC[i+5]; // ( also tried lsb/msb )
            sub[2]=SRC[i+1]; // skip +3, it's %
            sub[3]=SRC[i+2]; // 
            sub[4]='\0';
            i=i+5;
        } else {
            sub[0]=SRC[i+1];
            sub[1]=SRC[i+2];
            sub[2]='\0';
            i=i+2;
        }
        sscanf(&sub[0], "%x", &ii);
        ch=static_cast<wchar_t>(ii);
        ret+=ch;
    } else 
        ret+=SRC[i];

}
return ret;