在C ++中编码/解码网址 [英] Encode/Decode URLs in C++
本文介绍了在C ++中编码/解码网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道任何好的C ++代码吗?
Does anyone know of any good C++ code that does this?
推荐答案
天。对可用的选项不满意,看了一下这个C示例代码后,我决定滚动我自己的C ++ url-编码函数:
I faced the encoding half of this problem the other day. Unhappy with the available options, and after taking a look at this C sample code, i decided to roll my own C++ url-encode function:
#include <cctype>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
string url_encode(const string &value) {
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
}
return escaped.str();
}
解码功能的实现作为练习留给读者。 :P
The implementation of the decode function is left as an exercise to the reader. :P
这篇关于在C ++中编码/解码网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文