标签:style class blog c code ext
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节。Base64编码表
码值 | 字符 | 码值 | 字符 | 码值 | 字符 | 码值 | 字符 | |||
---|---|---|---|---|---|---|---|---|---|---|
0 | A | 16 | Q | 32 | g | 48 | w | |||
1 | B | 17 | R | 33 | h | 49 | x | |||
2 | C | 18 | S | 34 | i | 50 | y | |||
3 | D | 19 | T | 35 | j | 51 | z | |||
4 | E | 20 | U | 36 | k | 52 | 0 | |||
5 | F | 21 | V | 37 | l | 53 | 1 | |||
6 | G | 22 | W | 38 | m | 54 | 2 | |||
7 | H | 23 | X | 39 | n | 55 | 3 | |||
8 | I | 24 | Y | 40 | o | 56 | 4 | |||
9 | J | 25 | Z | 41 | p | 57 | 5 | |||
10 | K | 26 | a | 42 | q | 58 | 6 | |||
11 | L | 27 | b | 43 | r | 59 | 7 | |||
12 | M | 28 | c | 44 | s | 60 | 8 | |||
13 | N | 29 | d | 45 | t | 61 | 9 | |||
14 | O | 30 | e | 46 | u | 62 | + | |||
15 | P | 31 | f | 47 | v | 63 | / |
static const char s_acBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* * 函数名称: Base64Encode * 函数功能: base64编码 * 参 数: * pucIN - 编码前数据 * ulINLen - 编码前数据长度 * pucOUT - 编码后数据 * pucOUTLen - 编码后数据长度 * 返 回 值: 成功返回R__OK,否则返回相应错误码 */ unsigned int Base64Encode(const unsigned char *pucIN, unsigned long ulINLen, unsigned char *pucOUT, unsigned long *pulOUTLen) { if (NULL == pucIN || NULL == pucOUT || NULL == pulOUTLen) { return -1; } unsigned long i, len2, leven; unsigned char *p; len2 = 4 * ((ulINLen + 2) / 3); if (*pulOUTLen < len2 + 1) { *pulOUTLen = len2 + 1; return -2; } p = pucOUT; leven = 3*(ulINLen / 3); for (i = 0; i < leven; i += 3) { *pucOUT++ = s_acBase64[(pucIN[0] >> 2) & 0x3F]; *pucOUT++ = s_acBase64[(((pucIN[0] & 3) << 4) + (pucIN[1] >> 4)) & 0x3F]; *pucOUT++ = s_acBase64[(((pucIN[1] & 0xf) << 2) + (pucIN[2] >> 6)) & 0x3F]; *pucOUT++ = s_acBase64[pucIN[2] & 0x3F]; pucIN += 3; } if (i < ulINLen) { unsigned a = pucIN[0]; unsigned b = (i+1 < ulINLen) ? pucIN[1] : 0; *pucOUT++ = s_acBase64[(a >> 2) & 0x3F]; *pucOUT++ = s_acBase64[(((a & 3) << 4) + (b >> 4)) & 0x3F]; *pucOUT++ = (i+1 < ulINLen) ? s_acBase64[(((b & 0xf) << 2)) & 0x3F] : '='; *pucOUT++ = '='; } *pucOUT = '\0'; *pulOUTLen = pucOUT - p; return 0; }
标签:style class blog c code ext
原文地址:http://blog.csdn.net/wenwei19861106/article/details/26678033