Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262854
  • 博文数量: 50
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 919
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-03 19:18
个人简介

非淡泊无以明志,非宁静无以致远

文章分类

全部博文(50)

文章存档

2018年(2)

2017年(6)

2016年(7)

2015年(10)

2014年(11)

2013年(14)

我的朋友

分类: C/C++

2013-12-10 15:57:36


  1. char* Base64Encode(const char *str,int length)
  2. {
  3.      static char base64_table[] =
  4.      { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  5.         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  6.         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  7.         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  8.         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '/0' };
  9.  
  10.      static char base64_pad = '=';
  11.      const char *current = ( const char*)str;
  12.      int i = 0;
  13.      char *result = ( char *)malloc(((length + 3 - length % 3) * 4 / 3 + 1) * sizeof(char));
  14.      while (length > 2)
  15.     {
  16.           /* keep going until we have less than 24 bits */
  17.           result[i++] = base64_table[current[0] >> 2];
  18.           result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
  19.           result[i++] = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
  20.           result[i++] = base64_table[current[2] & 0x3f];
  21.           current += 3;
  22.           length -= 3; /* we just handle 3 octets of data */
  23.       }
  24.      /* now deal with the tail end of things */
  25.      if (length != 0)
  26.      {
  27.           result[i++] = base64_table[current[0] >> 2];
  28.           if (length > 1)
  29.           {
  30.                result[i++] = base64_table[((current[0] & 0x03 ) << 4) + (current[1] >> 4)];
  31.                result[i++] = base64_table[(current[1] & 0x0f) << 2];
  32.                result[i++] = base64_pad;
  33.           }
  34.           else
  35.           {
  36.                result[i++] = base64_table[(current[0] & 0x03) << 4];
  37.                result[i++] = base64_pad;
  38.                result[i++] = base64_pad;
  39.           }
  40.      }
  41.      result[i] = '/0'; // printf("%s/n",result);
  42.      return result;
  43. }


阅读(3521) | 评论(0) | 转发(0) |
0

上一篇:Linux下打包命令tar

下一篇:sed命令详解

给主人留下些什么吧!~~