服务器之家

服务器之家 > 正文

C++ 写的UrlEncode和UrlDecode实例

时间:2021-10-11 11:42     来源/作者:claireyuancy

关于UrlEncode的实现(C++)。网上有非常多不同的版本号。对须要编码的字符集的选取并不统一。那么究竟有没有标准呢?答案是有的。

绝对不编码的,仅仅有字母、数字、短横线(-)、下划线(_)、点(.)和波浪号(~),其它字符要视情况而定。所以一般性的urlencode仅仅需保留上述字符不进行编码。

以下给出实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
unsigned char ToHex(unsigned char x)
{
 return x > 9 ? x + 55 : x + 48;
}
 
unsigned char FromHex(unsigned char x)
{
 unsigned char y;
 if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
 else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
 else if (x >= '0' && x <= '9') y = x - '0';
 else assert(0);
 return y;
}
 
std::string UrlEncode(const std::string& str)
{
 std::string strTemp = "";
 size_t length = str.length();
 for (size_t i = 0; i < length; i++)
 {
  if (isalnum((unsigned char)str[i]) ||
   (str[i] == '-') ||
   (str[i] == '_') ||
   (str[i] == '.') ||
   (str[i] == '~'))
   strTemp += str[i];
  else if (str[i] == ' ')
   strTemp += "+";
  else
  {
   strTemp += '%';
   strTemp += ToHex((unsigned char)str[i] >> 4);
   strTemp += ToHex((unsigned char)str[i] % 16);
  }
 }
 return strTemp;
}
 
std::string UrlDecode(const std::string& str)
{
 std::string strTemp = "";
 size_t length = str.length();
 for (size_t i = 0; i < length; i++)
 {
  if (str[i] == '+') strTemp += ' ';
  else if (str[i] == '%')
  {
   assert(i + 2 < length);
   unsigned char high = FromHex((unsigned char)str[++i]);
   unsigned char low = FromHex((unsigned char)str[++i]);
   strTemp += high*16 + low;
  }
  else strTemp += str[i];
 }
 return strTemp;
}

补充知识:C++中URL解码/编码

我就废话不多说了,大家还是直接看代码吧~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
using namespace std;
 
char dec2hexChar(short int n) {
    if (0 <= n && n <= 9) {
        return char(short('0') + n);
    }
    else if (10 <= n && n <= 15) {
        return char(short('A') + n - 10);
    }
    else {
        return char(0);
    }
}
 
short int hexChar2dec(char c) {
    if ('0' <= c && c <= '9') {
        return short(c - '0');
    }
    else if ('a' <= c && c <= 'f') {
        return (short(c - 'a') + 10);
    }
    else if ('A' <= c && c <= 'F') {
        return (short(c - 'A') + 10);
    }
    else {
        return -1;
    }
}
 
string escapeURL(const string& URL)
{
    string result = "";
    for (unsigned int i = 0; i < URL.length(); i++) {
        char c = URL[i];
        if (
            ('0' <= c && c <= '9') ||
            ('a' <= c && c <= 'z') ||
            ('A' <= c && c <= 'Z') ||
            c == '/' || c == '.'
            ) {
            result += c;
        }
        else {
            int j = (short int)c;
            if (j < 0) {
                j += 256;
            }
            int i1, i0;
            i1 = j / 16;
            i0 = j - i1 * 16;
            result += '%';
            result += dec2hexChar(i1);
            result += dec2hexChar(i0);
        }
    }
    return result;
}
 
string deescapeURL(const string& URL) {
    string result = "";
    for (unsigned int i = 0; i < URL.length(); i++) {
        char c = URL[i];
        if (c != '%') {
            result += c;
        }
        else {
            char c1 = URL[++i];
            char c0 = URL[++i];
            int num = 0;
            num += hexChar2dec(c1) * 16 + hexChar2dec(c0);
            result += char(num);
        }
    }
    return result;
}

以上这篇C++ 写的UrlEncode和UrlDecode实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/claireyuancy/p/6915447.html

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部