- //URL 编码
- std::string urlencode(std::string encode)
- {
- std::string result;
- for(unsigned int i = 0; i< static_cast<unsigned int>(encode.length()); i++)
- {
- char ch = encode[i];
- if(ch == ' ')
- {
- result += '+';
- }else if(ch >= 'A' && ch <= 'Z'){
- result += ch;
- }else if(ch >= 'a' && ch <= 'z'){
- result += ch;
- }else if(ch >= '0' && ch <= '9'){
- result += ch;
- }else if(ch == '-' || ch == '-' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\\'' || ch == '(' || ch == ')' ){
- result += ch;
- }else{
- result += '%';
- result += iChoo::iconv::char_to_hex(ch);
- }
- }
- return result;
- }
- //URL 解码
- std::string urldecode(std::string decode)
- {
- std::string result;
- for(unsigned int i = 0; i< static_cast<unsigned int>(decode.length()); i++)
- {
- switch(decode[i])
- {
- case '+':
- result += ' ';
- break;
- case '%':
- if(isxdigit(decode[i + 1]) && isxdigit(decode[i + 2]))
- {
- result += iChoo::iconv::hex_to_char(decode[i+1], decode[i+2]);
- i += 2;
- }else {
- result += '%';
- }
- break;
- default:
- result += decode[i];
- break;
- }
- }
- return result;
- }
- //该片段来自于http://www.codesnippet.cn/detail/23112012613.html
来源: http://www.codesnippet.cn/detail/23112012613.html