zoukankan      html  css  js  c++  java
  • 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!

     

     

    作者:小乌龟
    【转载请注明出处,欢迎转载】 希望这篇文章能帮到你

     

  • 相关阅读:
    java基础知识(一)- 数据类型
    Java基础知识-去重
    java基础知识-冒泡排序
    Java排序方法sort的使用详解
    Java面试题-字符串操作
    Map.Entry遍历Map
    Java中String的用法
    Java循环控制语句-switch
    API文档打开显示'已取消到该网页的导航'的解决方法
    CentOS6.5 安装snort
  • 原文地址:https://www.cnblogs.com/music-liang/p/15656218.html
Copyright © 2011-2022 走看看