1. 程式人生 > 其它 >base16 編碼 和 解碼

base16 編碼 和 解碼

/**
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 
 * in compliance with the License. You may obtain a copy of the License at
 *
 * 
https://opensource.org/licenses/BSD-3-Clause * * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License.
*/

解碼的程式碼,參考了騰訊開源框架tars工程的 tc_cgi.cpp 程式碼

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 
 6 //16進位制轉為char 
 7 char x2c(const string& sWhat)
 8 {
 9     register char digit;
10 
11     if (sWhat.length() < 2)
12     {
13         return '\0';
14     }
15 
16     digit = (sWhat[0
] >= 'A' ? ((sWhat[0] & 0xdf) - 'A') + 10 : (sWhat[0] - '0')); 17 digit *= 16; 18 digit += (sWhat[1] >= 'A' ? ((sWhat[1] & 0xdf) - 'A') + 10 : (sWhat[1] - '0')); 19 20 return (digit); 21 } 22 23 24 25 string Base16Decode(const string& sUrl) 26 { 27 string sDecodeUrl; 28 register string::size_type pos = 0; 29 string::size_type len = sUrl.length(); 30 31 sDecodeUrl = ""; 32 33 while (pos < len) 34 { 35 if (sUrl[pos] == '+') 36 { 37 sDecodeUrl += ' '; 38 ++pos; 39 } 40 else if (sUrl[pos] == '%') 41 { 42 sDecodeUrl += x2c(sUrl.substr(pos + 1)); 43 pos += 3; 44 } 45 else 46 { 47 sDecodeUrl += sUrl[pos]; 48 49 ++pos; 50 } 51 } 52 53 return sDecodeUrl; 54 } 55 56 string Base16Encode(const string& sUrl) 57 { 58 static char HEX_TABLE[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 59 60 string result; 61 for (size_t i = 0; i < sUrl.length(); i++) 62 { 63 char c = sUrl[i]; 64 if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 65 result.append(1, c); 66 else 67 { 68 result.append(1, '%'); 69 result.append(1, HEX_TABLE[(c >> 4) & 0x0f]); 70 result.append(1, HEX_TABLE[c & 0x0f]); 71 } 72 } 73 74 return result; 75 } 76 77 int main() { 78 string str1 = "HenryAbc123"; 79 string str2 = "何亮到此一遊abcde123"; 80 81 string out1 = Base16Encode(str1); 82 string out2 = Base16Encode(str2); 83 cout << "=====================begin encode:" << endl; 84 cout << "out1:"<<out1 << endl; 85 cout << "out2:"<<out2 << endl; 86 cout << "=====================begin decode:" << endl; 87 88 string decodeStr1 = Base16Decode(out1); 89 string decodeStr2 = Base16Decode(out2); 90 cout << "decodeStr1:" << decodeStr1 << endl; 91 cout << "decodeStr2:" << decodeStr2 << endl; 92 93 94 cout << "Base 16 test end!" << endl; 95 return 0; 96 }

=======屏==幕==輸==出========= output:

=====================begin encode:
out1:HenryAbc123
out2:%BA%CE%C1%C1%B5%BD%B4%CB%D2%BB%D3%CEabcde123
=====================begin decode:
decodeStr1:HenryAbc123
decodeStr2:何亮到此一遊abcde123
Base 16 test end!

 

 

作者:小烏龜 出處:http://www.cnblogs.com/music-liang/ 【轉載請註明出處,歡迎轉載】 希望這篇文章能幫到你