随笔 - 741  文章 - 0  评论 - 260  阅读 - 416万

base64 encoding

//https://en.wikipedia.org/wiki/Base64

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

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


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;
}
void Cbase64Dlg::OnBnClickedButton1()
{
   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);
}

posted on   莫水千流  阅读(617)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2014-07-22 IP地址转换、主机大小端、htonl、ntohl实现
2014-07-22 判断系统大小端方法分析与总结
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示