BASE64编码解码算法

byte   table(byte   ot)  
  {  
  if(ot>=0   &&   ot<=25)return   a   +   ot;  
  else   if(ot>=26   &&   ot<=51)return   a   +   (ot-26);  
  else   if(ot>=52   &&   ot<=61)return   0   +   (ot-52);  
  else   if(ot==62)return   +;  
  else   if(ot==63)return   /;  
  else   return   =;  
  }  
   
  void   base64(char*   pszsource,   char*   pszcode)  
  {  
  byte   ot;  
  int   i;  
  i=0;  
  while(pszsource[i])  
  {  
  ot   =   (0xfc   &   pszsource[i])>>2;   *pszcode   =   table(ot);   i++;   pszcode++;  
  if(!pszsource[i])  
  {  
  ot   =   (0x03   &   pszsource[i-1])<<4;   *pszcode   =   table(ot);   pszcode++;  
  break;  
  }  
  ot   =   (0x03   &   pszsource[i-1])<<4   |   (0xf0   &   pszsource[i])>>4;   *pszcode   =   table(ot);   pszcode++;   i++;  
  if(!pszsource[i])  
  {  
  ot   =   (0x0f   &   pszsource[i-1])<<2;   *pszcode   =   table(ot);   pszcode++;  
  break;  
  }  
  ot   =   (0x0f   &   pszsource[i-1])<<2   |   (0xc0   &   pszsource[i])>>6;   *pszcode   =   table(ot);   pszcode++;  
  ot   =   0x3f   &   pszsource[i];   *pszcode   =   table(ot),   i++;   pszcode++;  
  }  
  *pszcode   =   null;  
  }  
   
  byte   getsixbits(char*   pszstr,   int&   i)  
  {  
  char   c;  
   
  while(1)  
  {  
  if(i>=strlen(pszstr))break;  
  c   =   pszstr[i++];  
  if(isupper(c))   return   c-a;  
  else   if(islower(c))   return   c-a+26;  
  else   if(isdigit(c))   return   c-0+52;  
  else   if(c==+)   return   62;  
  else   if(c==/)   return   63;  
  else   if(c===)   return   0;  
  }  
  return   0;  
  }  
   
  void   unbase64(char*   psztext,   char*   pszcode)  
  {  
  int   nlen,   i;  
  byte   bysixbits0,   bysixbits1,   bysixbits2,   bysixbits3,   c;  
   
  nlen   =   strlen(pszcode);  
  for(i=0;   i<nlen;   )  
  {  
  bysixbits0   =   getsixbits(pszcode,   i);  
  if(i<nlen)bysixbits1   =   getsixbits(pszcode,   i);  
  if(i<nlen)bysixbits2   =   getsixbits(pszcode,   i);  
  if(i<nlen)bysixbits3   =   getsixbits(pszcode,   i);  
   
  c   =   bysixbits0<<2   |   bysixbits1>>4;   *psztext   =   c;   psztext++;  
  c   =   bysixbits1<<4   |   bysixbits2>>2;   *psztext   =   c;   psztext++;  
  c   =   bysixbits2<<6   |   bysixbits3;   *psztext   =   c;   psztext++;  
  }  
  *psztext   =   null;  
   
   
  }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值