關(guān)于UrlEncode的實(shí)現(xiàn)(C++)。網(wǎng)上有非常多不同的版本號(hào)。對(duì)須要編碼的字符集的選取并不統(tǒng)一。那么究竟有沒(méi)有標(biāo)準(zhǔn)呢?答案是有的。
絕對(duì)不編碼的,僅僅有字母、數(shù)字、短橫線(-)、下劃線(_)、點(diǎn)(.)和波浪號(hào)(~),其它字符要視情況而定。所以一般性的urlencode僅僅需保留上述字符不進(jìn)行編碼。
以下給出實(shí)現(xiàn):
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; } |
補(bǔ)充知識(shí):C++中URL解碼/編碼
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
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實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/claireyuancy/p/6915447.html