转载:http://www.cnblogs.com/newcj/p/3645749.html
1 #include "UrlEncode.h" 2 #include <string> 3 #include <windows.h> 4 #include <assert.h> 5 6 using namespace std; 7 8 namespace ConnectSDK 9 { 10 namespace Utility 11 { 12 const wchar_t * hexenc[] = { 13 L"%00", L"%01", L"%02", L"%03", L"%04", L"%05", L"%06", L"%07", 14 L"%08", L"%09", L"%0a", L"%0b", L"%0c", L"%0d", L"%0e", L"%0f", 15 L"%10", L"%11", L"%12", L"%13", L"%14", L"%15", L"%16", L"%17", 16 L"%18", L"%19", L"%1a", L"%1b", L"%1c", L"%1d", L"%1e", L"%1f", 17 L"%20", L"%21", L"%22", L"%23", L"%24", L"%25", L"%26", L"%27", 18 L"%28", L"%29", L"%2a", L"%2b", L"%2c", L"%2d", L"%2e", L"%2f", 19 L"%30", L"%31", L"%32", L"%33", L"%34", L"%35", L"%36", L"%37", 20 L"%38", L"%39", L"%3a", L"%3b", L"%3c", L"%3d", L"%3e", L"%3f", 21 L"%40", L"%41", L"%42", L"%43", L"%44", L"%45", L"%46", L"%47", 22 L"%48", L"%49", L"%4a", L"%4b", L"%4c", L"%4d", L"%4e", L"%4f", 23 L"%50", L"%51", L"%52", L"%53", L"%54", L"%55", L"%56", L"%57", 24 L"%58", L"%59", L"%5a", L"%5b", L"%5c", L"%5d", L"%5e", L"%5f", 25 L"%60", L"%61", L"%62", L"%63", L"%64", L"%65", L"%66", L"%67", 26 L"%68", L"%69", L"%6a", L"%6b", L"%6c", L"%6d", L"%6e", L"%6f", 27 L"%70", L"%71", L"%72", L"%73", L"%74", L"%75", L"%76", L"%77", 28 L"%78", L"%79", L"%7a", L"%7b", L"%7c", L"%7d", L"%7e", L"%7f", 29 L"%80", L"%81", L"%82", L"%83", L"%84", L"%85", L"%86", L"%87", 30 L"%88", L"%89", L"%8a", L"%8b", L"%8c", L"%8d", L"%8e", L"%8f", 31 L"%90", L"%91", L"%92", L"%93", L"%94", L"%95", L"%96", L"%97", 32 L"%98", L"%99", L"%9a", L"%9b", L"%9c", L"%9d", L"%9e", L"%9f", 33 L"%a0", L"%a1", L"%a2", L"%a3", L"%a4", L"%a5", L"%a6", L"%a7", 34 L"%a8", L"%a9", L"%aa", L"%ab", L"%ac", L"%ad", L"%ae", L"%af", 35 L"%b0", L"%b1", L"%b2", L"%b3", L"%b4", L"%b5", L"%b6", L"%b7", 36 L"%b8", L"%b9", L"%ba", L"%bb", L"%bc", L"%bd", L"%be", L"%bf", 37 L"%c0", L"%c1", L"%c2", L"%c3", L"%c4", L"%c5", L"%c6", L"%c7", 38 L"%c8", L"%c9", L"%ca", L"%cb", L"%cc", L"%cd", L"%ce", L"%cf", 39 L"%d0", L"%d1", L"%d2", L"%d3", L"%d4", L"%d5", L"%d6", L"%d7", 40 L"%d8", L"%d9", L"%da", L"%db", L"%dc", L"%dd", L"%de", L"%df", 41 L"%e0", L"%e1", L"%e2", L"%e3", L"%e4", L"%e5", L"%e6", L"%e7", 42 L"%e8", L"%e9", L"%ea", L"%eb", L"%ec", L"%ed", L"%ee", L"%ef", 43 L"%f0", L"%f1", L"%f2", L"%f3", L"%f4", L"%f5", L"%f6", L"%f7", 44 L"%f8", L"%f9", L"%fa", L"%fb", L"%fc", L"%fd", L"%fe", L"%ff" 45 }; 46 47 String^ UrlEncode(String^ url) 48 { 49 wstring text(url->Data()); 50 51 size_t len = text.length(); 52 std::wstring encoded = L""; 53 for(size_t i = 0; i < len; i++) 54 { 55 wchar_t wch = text.at(i); 56 if ('A' <= wch && wch <= 'Z') { 57 encoded += wch; 58 } else if ('a' <= wch && wch <= 'z') { 59 encoded += wch; 60 } else if ('0' <= wch && wch <= '9') { 61 encoded += wch; 62 } else if (wch == ' ') { 63 encoded += hexenc[wch]; 64 } else if (wch == '-' || wch == '_' 65 || wch == '.' || wch == '!' 66 || wch == '~' || wch == '*' 67 || wch == '\'' || wch == '(' 68 || wch == ')') { 69 encoded += hexenc[wch]; 70 } else if (wch <= 0x007f) { // other ASCII 71 encoded += hexenc[wch]; 72 } else if (wch <= 0x07FF) { // non-ASCII <= 0x7FF 73 encoded += hexenc[0xc0 | (wch >> 6)]; 74 encoded += hexenc[0x80 | (wch & 0x3F)]; 75 } else { // 0x7FF < ch <= 0xFFFF 76 encoded += hexenc[0xe0 | (wch >> 12)]; 77 encoded += hexenc[0x80 | ((wch >> 6) & 0x3F)]; 78 encoded += hexenc[0x80 | (wch & 0x3F)]; 79 } 80 } 81 return ref new String(encoded.c_str()); 82 } 83 84 String^ UrlDecode(String^ encodeUrl) 85 { 86 wstring text(encodeUrl->Data()); 87 std::wstring decoded = L""; 88 wchar_t temp[] = L"0x00"; 89 size_t len = text.length(); 90 int sequence = 0; 91 wchar_t conwch = 0; 92 for(size_t i = 0; i < len; i++) 93 { 94 wchar_t wch = text.at(i++); 95 if((wch == '%') && (i+1 < len)) 96 { 97 temp[2] = text.at(i++); 98 temp[3] = text.at(i); 99 long tconwch = wcstol(temp, NULL, 16); 100 if(tconwch <= 0x7F) { 101 decoded += tconwch; // normal ascii char 102 } else if(tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte 103 tconwch = tconwch & 0x3F; 104 if(sequence-- == 2) 105 tconwch = tconwch << 6; 106 conwch |= tconwch; 107 if(sequence == 0) 108 decoded += conwch; 109 } else if(tconwch >= 0xC0 && tconwch <= 0xDF) { 110 conwch = (tconwch & 0x1F) << 6; // make space for partial bytes 111 sequence = 1; // 1 more partial bytes follow 112 } else if(tconwch >= 0xE0 && tconwch <= 0xEF) { 113 conwch = (tconwch & 0xF) << 12; // make space for partial bytes 114 sequence = 2; // 2 more partial bytes follow 115 } // TODO add case fore 3 partial bytes ... very rare 116 } else { 117 decoded += text.at(--i); 118 } 119 } 120 return ref new String(decoded.c_str()); 121 } 122 } 123 }
上面是网上找的:
根据项目需要进行修改:
UrlEncode:
1 CString URLEncode(CString url) 2 { 3 std::wstring text = url; 4 5 size_t len = text.length(); 6 std::wstring encoded = L""; 7 for(size_t i = 0; i < len; i++) 8 { 9 wchar_t wch = text.at(i); 10 if ('A' <= wch && wch <= 'Z') { 11 encoded += wch; 12 } else if ('a' <= wch && wch <= 'z') { 13 encoded += wch; 14 } else if ('0' <= wch && wch <= '9') { 15 encoded += wch; 16 } else if (wch == ' ') { 17 encoded += hexenc[wch]; 18 } else if (wch == '-' || wch == '_' 19 || wch == '.' || wch == '!' 20 || wch == '~' || wch == '*' 21 || wch == '\'' || wch == '(' 22 || wch == ')') { 23 encoded += hexenc[wch]; 24 } else if (wch <= 0x007f) { // other ASCII 25 encoded += hexenc[wch]; 26 } else if (wch <= 0x07FF) { // non-ASCII <= 0x7FF 27 encoded += hexenc[0xc0 | (wch >> 6)]; 28 encoded += hexenc[0x80 | (wch & 0x3F)]; 29 } else { // 0x7FF < ch <= 0xFFFF 30 encoded += hexenc[0xe0 | (wch >> 12)]; 31 encoded += hexenc[0x80 | ((wch >> 6) & 0x3F)]; 32 encoded += hexenc[0x80 | (wch & 0x3F)]; 33 } 34 } 35 return encoded.c_str(); 36 }
UrlDecode:
1 CString UrlDecode(CString encodeUrl) 2 { 3 std::wstring text = encodeUrl; 4 std::wstring decoded = L""; 5 wchar_t temp[] = L"0x00"; 6 size_t len = text.length(); 7 int sequence = 0; 8 wchar_t conwch = 0; 9 for(size_t i = 0; i < len; i++) 10 { 11 wchar_t wch = text.at(i++); 12 if((wch == '%') && (i+1 < len)) 13 { 14 temp[2] = text.at(i++); 15 temp[3] = text.at(i); 16 long tconwch = wcstol(temp, NULL, 16); 17 if(tconwch <= 0x7F) { 18 decoded += tconwch; // normal ascii char 19 } else if(tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte 20 tconwch = tconwch & 0x3F; 21 if(sequence-- == 2) 22 tconwch = tconwch << 6; 23 conwch |= tconwch; 24 if(sequence == 0) 25 decoded += conwch; 26 } else if(tconwch >= 0xC0 && tconwch <= 0xDF) { 27 conwch = (tconwch & 0x1F) << 6; // make space for partial bytes 28 sequence = 1; // 1 more partial bytes follow 29 } else if(tconwch >= 0xE0 && tconwch <= 0xEF) { 30 conwch = (tconwch & 0xF) << 12; // make space for partial bytes 31 sequence = 2; // 2 more partial bytes follow 32 } // TODO add case fore 3 partial bytes ... very rare 33 } else { 34 decoded += text.at(--i); 35 } 36 } 37 return decoded.c_str(); 38 }