现在需要对一个图片编码后的字符串进行解码,这个图片编码后的字符串我存在一个txt文件中,然后在程序中读取这个txt文件,用一个长度为30000的字符数组来存放它,这个字符串差不多有两万多位,在网上找了一个base64的解码方法,代码如下:
[code=c]static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *decode(const char *base64Char, const long base64CharSize, char *originChar, long originCharSize) {
int toInt[128] = { -1 };
for (int i = 0; i < 64; i++) {
toInt[ALPHA_BASE[i]] = i;
}
int int255 = 0xFF;
int index = 0;
for (int i = 0; i < base64CharSize; i += 4) {
int c0 = toInt[base64Char[i]];
int c1 = toInt[base64Char[i + 1]];
originChar[index++] = (((c0 << 2) | (c1 >> 4)) & int255);
if (index >= originCharSize) {
return originChar;
}
int c2 = toInt[base64Char[i + 2]];
originChar[index++] = (((c1 << 4) | (c2 >> 2)) & int255);
if (index >= originCharSize) {
return originChar;
}
int c3 = toInt[base64Char[i + 3]];
originChar[index++] = (((c2 << 6) | c3) & int255);
}
return originChar;
}[/code]
现在的问题是,我用这个函数对字符串进行解码后,显示的解码后的字符串的长度才11,解码后的源码也就是几个乱码,想问一下这是什么问题?是解码函数的问题还是什么?如果是解码函数的问题能提供一个经过测试的解码函数吗?在这里先谢了。
[code=c]FILE *in = fopen("g:\test.txt","r");
char line[30000];
char *line_temp=malloc(30000*sizeof(char));
fgets(line, sizeof(line), in);
//printf("%s", line);
printf("编码字符串的长度为%d\n",strlen(line));
decode(line, strlen(line), line_temp, len);
printf("解码后的源码长度为:%d\n", strlen(line_temp));
printf("解码后的字符串为%s\n", line_temp);[/code]