zoukankan      html  css  js  c++  java
  • Base64编码解码

    what is base64 coding and decoding is in reference to

    https://en.wikipedia.org/wiki/Base64
    http://www.cnblogs.com/chengxiaohui/articles/3951129.html
      

    //sample code in c++. Please note that the code needs refinements as there is some warning in some analysis tools,e.g. pc-lint, Coverity etc.

    // it is just a sample code for study.

    //declaration in header

    std::string base64Encode(const std::vector<char>& byteData);
    std::vector<char> base64Decode(std::string & const inputString);

    //implemenation

    std::string Cbase64Dlg::base64Encode(const std::vector<char>& byteData)
    {
       const std::string codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
       std::string base64String;
       int b;
       for (size_t i = 0; i < byteData.size(); i = i+3)
       {
          b = (byteData[i] & 0xFC) >> 2;
          base64String.push_back(codes[b]);
          b = (byteData[i] & 0x03) << 4;
          if (i + 1 < byteData.size())
          {
             b |= (byteData[i + 1] & 0xF0) >> 4;
             base64String.push_back(codes[b]);
             b = (byteData[i + 1] & 0x0F) << 2;
             if (i+2 < byteData.size())
             {
                b |= (byteData[i + 2] & 0xC0) >> 6;
                base64String.push_back(codes[b]);
                b = byteData[i + 2] & 0x3F;
                base64String.push_back(codes[b]);
             }
             else
             {
                base64String.push_back(codes[b]);
                base64String.append("=");
             }
          }
          else
          {
             base64String.push_back(codes[b]);
             base64String.append("==");
          }
       }
       
       return base64String;
    }

    std::vector<char> Cbase64Dlg::base64Decode(std::string & const inputString)
    {   
       static std::string codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
       std::vector<char> decoded;

       if (inputString.length() % 4 != 0)    
       {
          return std::vector<char>();
       }

       //The ratio of output bytes to input bytes is 4:3
       int outLen = (inputString.length() * 3 / 4);

       size_t pos = inputString.find_first_of('=');
       if (pos != std::string::npos)
       {
          decoded.resize(outLen - (inputString.length() - pos));
       }
       else
       {
          decoded.resize(outLen);
       }

       int j = 0;
       int b[4] = {};

       const char* p = inputString.c_str();

       while(p && *p && j < outLen)
       {
          bool valid = false;
          for (int i=0; p && i < 4; ++i)
          {
             size_t pos = codes.find_first_of(*p++);
             if ( pos != std::string::npos)
             {
                b[i] = pos;
             }
          }

          if (j < outLen)
          {
             decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));;
             if (j < outLen && b[2] < 64)      
             {
                decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));

                if (j < outLen && b[3] < 64)  
                {
                   decoded[j++] = (byte) ((b[2] << 6) | b[3]);
                }
             }
          }
       }

       return decoded;
    }

    // test code

       char myints[] = "ABC&&&&&&&&&&";
       std::vector<char> byte (myints, myints + sizeof(myints) / sizeof(char) );
       std::string value = base64Encode(byte);
       std::cout << value << std::endl;
       std::vector<char>decode = base64Decode(value);

  • 相关阅读:
    hdu 5115 Dire Wolf 区间DP
    泛型兼容的注意事项
    maven web项目不能创建src/main/java等文件夹的问题
    error the @annotation pointcut expression is only supported at Java 5
    HashSet重复元素判断
    oracle 分库分表(sharding)
    关系型数据库分库分表解决方案
    JDK7中匿名内部类中使用局部变量要加final,JDK8中不需要,但jdk会默认加上final
    eclipse中更改配置使得switch语句不出错
    java io流中怎么在一个文本中追加字符串
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/5040772.html
Copyright © 2011-2022 走看看