请帮我写一下base64解码 的C语言代码
时间: 2023-04-05 10:05:16 浏览: 129
以下是C语言的base64解码代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int base64_decode(const char *src, unsigned char *dst, int len)
{
int i, j, k;
unsigned char c;
unsigned char *p = dst;
for (i = 0; i < len; i += 4) {
k = 0;
for (j = 0; j < 4; j++) {
c = src[i + j];
if (c == '=') {
k++;
continue;
}
c = strchr(base64_table, c) - base64_table;
if (c == (unsigned char)-1) {
return -1;
}
switch (j) {
case 0:
*p++ = (c << 2);
break;
case 1:
*p++ |= (c >> 4);
*p++ = (c << 4);
break;
case 2:
*p++ |= (c >> 2);
*p++ = (c << 6);
break;
case 3:
*p++ |= c;
break;
}
}
p -= k;
}
return p - dst;
}
int main()
{
const char *src = "SGVsbG8gV29ybGQh";
unsigned char dst[1024];
int len;
len = base64_decode(src, dst, strlen(src));
if (len < 0) {
printf("base64_decode failed\n");
return -1;
}
printf("decoded: %.*s\n", len, dst);
return 0;
}
```
注意:这只是一个示例代码,实际应用中需要根据具体情况进行修改和优化。
阅读全文