编码的过程是这样的:
第一个字符通过右移2位获得第一个目标 字符的Base64表位置,根据这个数值取到表上相应的字符,就是第一个目标字符。
然后将第一个字符左移4位加上第二个字符右移4位,即获得第二 个目标字符。
再将第二个字符左移2位加上第三个字符右移6位,获得第三个目标字符。
最后取第三个字符的右6位即获得第四个目标字符。
在 以上的每一个步骤之后,再把结果与 0x3F 进行 AND 位操作,就可以得到编码后的字符了。
00001
00013 #include
00014
00015
00016 static const char base64digits[] =
00017 “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
00018
00019 #define BAD -1
00020 static const char base64val[] = {
00021 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
00022 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
00023 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
00024 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
00025 BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
00026 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD,
00027 BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
00028 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD
00029 };
00030 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
00031
00039 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
00040 {
00041 for (; inlen >= 3; inlen -= 3)
00042 {
00043 *out++ = base64digits[in[0] >> 2];
00044 *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
00045 *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
00046 *out++ = base64digits[in[2] & 0x3f];
00047 in += 3;
00048 }
00049
00050 if (inlen > 0)
00051 {
00052 unsigned char fragment;
00053
00054 *out++ = base64digits[in[0] >> 2];
00055 fragment = (in[0] << 4) & 0x30;
00056
00057 if (inlen > 1)
00058 fragment |= in[1] >> 4;
00059
00060 *out++ = base64digits[fragment];
00061 *out++ = (inlen < 2) ? ‘=’ : base64digits[(in[1] << 2) & 0x3c];
00062 *out++ = ‘=’;
00063 }
00064
00065 *out = ‘\0’;
00066 }
00067
00075 int from64tobits(char *out, const char *in)
00076 {
00077 int len = 0;
00078 register unsigned char digit1, digit2, digit3, digit4;
00079
00080 if (in[0] == ‘+’ && in[1] == ‘ ‘)
00081 in += 2;
00082 if (*in == ‘\r’)
00083 return(0);
00084
00085 do {
00086 digit1 = in[0];
00087 if (DECODE64(digit1) == BAD)
00088 return(-1);
00089 digit2 = in[1];
00090 if (DECODE64(digit2) == BAD)
00091 return(-1);
00092 digit3 = in[2];
00093 if (digit3 != ‘=’ && DECODE64(digit3) == BAD)
00094 return(-1);
00095 digit4 = in[3];
00096 if (digit4 != ‘=’ && DECODE64(digit4) == BAD)
00097 return(-1);
00098 in += 4;
00099 *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
00100 ++len;
00101 if (digit3 != ‘=’)
00102 {
00103 *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
00104 ++len;
00105 if (digit4 != ‘=’)
00106 {
00107 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
00108 ++len;
00109 }
00110 }
00111 } while (*in && *in != ‘\r’ && digit4 != ‘=’);
00112
00113 return (len);
00114 }
from http://blog.csdn.net/xueleng/archive/2006/03/24/637561.aspx

延伸阅读
- javascript实现Base64编码与解码函数
- 不引用外部图片文件,实现往网页html里插入图片
- JavaScript 中的 Base64 编码Encoding
- JavaScript与PHP之间中文base64码转换源代码
- asp/vbscript版Base64函数/Base64编码Base64encode解码Base64decode
- 浅谈Base64编码
- UTF-8编码 简介
- iconv转换UTF-8编码出错: iconv: 未知 xxx 处的非法输入序列
- 中文化和国际化问题权威解析之一:字符编码发展历程
- php使用iconv进行从utf-8转为gb2312字符编码出错解决方案
- ASCII字符编码表/扩展的ASCII编码表
- 趣话字符集与字符编码
- 字符编码的奥秘
- 扩展的 ASCII 字符转化表
- 常见字符编码类型 汇总比较表
0 条评论。