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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| char hexVals[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; string CURLEncode::csUnsafeString= "\"<>%\\^[]`+$,@:;/!#?=&"; string convert(char val) { string csRet; csRet += "%"; csRet += decToHex(val, 16); return csRet; } string decToHex(char num, int radix) { int temp=0; string csTmp; int num_char; num_char = (int) num; if (num_char < 0) num_char = 256 + num_char; while (num_char >= radix) { temp = num_char % radix; num_char = (int)floor((num_char / radix) * 1.0); csTmp = hexVals[temp]; } csTmp += hexVals[num_char]; if(csTmp.length() < 2) { csTmp += '0'; } string strdecToHex = csTmp; std::reverse(strdecToHex.begin(), strdecToHex.end()); return strdecToHex; } bool isUnsafe(char compareChar) { bool bcharfound = false; char tmpsafeChar; int m_strLen = 0; m_strLen = csUnsafeString.length(); for(int ichar_pos = 0; ichar_pos < m_strLen ;ichar_pos++) { tmpsafeChar = csUnsafeString[ichar_pos]; if(tmpsafeChar == compareChar) { bcharfound = true; break; } } int char_ascii_value = 0; char_ascii_value = (int) compareChar; if(bcharfound == false && char_ascii_value > 32 && char_ascii_value < 123) { return false; } else { return true; } return true; } string URLEncode(string strEncode) { string strSrc; string strDest; strSrc = strEncode; for(int i = 0; i < strSrc.length(); i++) { char ch = strSrc[i]; if (ch < ' ') { ch = ch; } if(!isUnsafe(ch)) { strDest += ch; } else { strDest += convert(ch); } } return strDest; }
|