base64位编解码函数
复制代码
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131void encode(byte* input ,byte*& output) { char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int len = strlen((char*)input); int remain = len % 3; if (remain == 0) { output = new byte[len / 3 * 4 + 1]; memset(output, 0, len / 3 * 4 + 1); for (int i = 0, j = 0; i<len; i += 3, j += 4) { byte a1 = (input[i] >> 2) & 0x3f; byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f; byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f; byte a4 = (input[i + 2] << 2 >> 2) & 0x3f; output[j] = arr[a1]; output[j + 1] = arr[a2]; output[j + 2] = arr[a3]; output[j + 3] = arr[a4]; } } else if (remain == 1) { output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1); int i = 0, j = 0; for (; i<len - 3; i += 3, j += 4) { byte a1 = (input[i] >> 2) & 0x3f; byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f; byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f; byte a4 = (input[i + 2] << 2 >> 2) & 0x3f; output[j] = arr[a1]; output[j + 1] = arr[a2]; output[j + 2] = arr[a3]; output[j + 3] = arr[a4]; } byte a1 = (input[i] >> 2) & 0x3f; byte a2 = (((input[i] << 6) | 0x00) >> 2) & 0x3f; output[j] = arr[a1]; output[j + 1] = arr[a2]; output[j + 2] = '='; output[j + 3] = '='; } else if (remain == 2) { output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1); int i = 0, j = 0; for (; i<len - 3; i += 3, j += 4) { byte a1 = (input[i] >> 2) & 0x3f; byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f; byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f; byte a4 = (input[i + 2] << 2 >> 2) & 0x3f; output[j] = arr[a1]; output[j + 1] = arr[a2]; output[j + 2] = arr[a3]; output[j + 3] = arr[a4]; } byte a1 = (input[i] >> 2) & 0x3f; byte a2 = (((input[i] << 6) | input[i + 1] >> 4 << 2) >> 2) & 0x3f; byte a3 = (((input[i + 1] << 4) | 0x00) >> 2) & 0x3f; output[j] = arr[a1]; output[j + 1] = arr[a2]; output[j + 2] = arr[a3]; output[j + 3] = '='; } } byte find(byte c) { char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; for (byte i = 0; i<64; i++) { if (arr[i] == c) { return i; } } } void decode(byte* input, byte*& output) { int len = strlen((char*)input); if (input[len - 1] != '=') { output = new byte[len / 4 * 3 + 1]; memset(output, 0, len / 4 * 3 + 1); for (int i = 0, j = 0; i<len; i += 4, j += 3) { byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4); byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2); byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]); output[j] = a1; output[j + 1] = a2; output[j + 2] = a3; } } else if (input[len - 1] == '='&&input[len - 2] == '=') { output = new byte[len / 4 * 3 - 2 + 1]; memset(output, 0, len / 4 * 3 - 2 + 1); int i = 0, j = 0; for (; i<len - 4; i += 4, j += 3) { byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4); byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2); byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]); output[j] = a1; output[j + 1] = a2; output[j + 2] = a3; } byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4); output[j] = a1; } else if (input[len - 1] == '='&&input[len - 2] != '=') { output = new byte[len / 4 * 3 - 1 + 1]; memset(output, 0, len / 4 * 3 - 1 + 1); int i = 0, j = 0; for (; i<len - 4; i += 4, j += 3) { byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4); byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2); byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]); output[j] = a1; output[j + 1] = a2; output[j + 2] = a3; } byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4); byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2); output[j] = a1; output[j + 1] = a2; } }
最后
以上就是英勇仙人掌最近收集整理的关于base64编码解码函数的全部内容,更多相关base64编码解码函数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复