user-0 提问: C语言如何对图片base64编码后的字符串进行解码？

回复:
user: user-0
content: 引用 9 楼 zwfgdlc的回复:[quote=引用 8 楼 lonmecy 的回复:] [quote=引用 7 楼 zwfgdlc 的回复:]  #include <atlenc.h>  	FILE* fp; 	fopen_s(&fp, "h:\\123.jpg", "rb"); 	fseek(fp, 0, SEEK_END); 	UINT FileSize = ftell(fp); 	PVOID pbuf = malloc(FileSize); 	fseek(fp, 0, SEEK_SET); 	fread(pbuf, FileSize, 1, fp); 	fclose(fp);  	int Base64EncLen = Base64EncodeGetRequiredLength(FileSize, ATL_BASE64_FLAG_NOCRLF); 	PCHAR szEncBase64 = (PCHAR)malloc(Base64EncLen); 	Base64Encode((PBYTE)pbuf, FileSize, szEncBase64, &Base64EncLen, ATL_BASE64_FLAG_NOCRLF);  	int Base64DecLen = Base64DecodeGetRequiredLength(Base64EncLen); 	PVOID pDebuf = malloc(Base64DecLen); 	Base64Decode(szEncBase64, Base64EncLen, (PBYTE)pDebuf, &Base64DecLen);  	FILE* file; 	fopen_s(&file, "d:\\1.jpg", "wb"); 	fwrite(pDebuf, Base64DecLen, 1, file); 	fclose(file);  	free(pbuf); 	free(szEncBase64); 	free(pDebuf); 中间的代码是放在main里运行吗？为什么我一用就报错啊，点开错误信息出了atlalloc.h和atlconv.h这两个头文件，显示了一大堆"应输入声明"。另外这种base64编码解码的原理是什么啊？有没有什么可供参考的资料啊。 [/quote] 什么编译器?[/quote] 问题已经解决了，之前报错是因为源文件是c文件而不是cpp文件，十分感谢

user: user-1
content: 引用 8 楼 lonmecy 的回复:[quote=引用 7 楼 zwfgdlc 的回复:]  #include <atlenc.h>  	FILE* fp; 	fopen_s(&fp, "h:\\123.jpg", "rb"); 	fseek(fp, 0, SEEK_END); 	UINT FileSize = ftell(fp); 	PVOID pbuf = malloc(FileSize); 	fseek(fp, 0, SEEK_SET); 	fread(pbuf, FileSize, 1, fp); 	fclose(fp);  	int Base64EncLen = Base64EncodeGetRequiredLength(FileSize, ATL_BASE64_FLAG_NOCRLF); 	PCHAR szEncBase64 = (PCHAR)malloc(Base64EncLen); 	Base64Encode((PBYTE)pbuf, FileSize, szEncBase64, &Base64EncLen, ATL_BASE64_FLAG_NOCRLF);  	int Base64DecLen = Base64DecodeGetRequiredLength(Base64EncLen); 	PVOID pDebuf = malloc(Base64DecLen); 	Base64Decode(szEncBase64, Base64EncLen, (PBYTE)pDebuf, &Base64DecLen);  	FILE* file; 	fopen_s(&file, "d:\\1.jpg", "wb"); 	fwrite(pDebuf, Base64DecLen, 1, file); 	fclose(file);  	free(pbuf); 	free(szEncBase64); 	free(pDebuf); 中间的代码是放在main里运行吗？为什么我一用就报错啊，点开错误信息出了atlalloc.h和atlconv.h这两个头文件，显示了一大堆"应输入声明"。另外这种base64编码解码的原理是什么啊？有没有什么可供参考的资料啊。 [/quote] 什么编译器?

user: user-0
content: 引用 7 楼 zwfgdlc 的回复: #include <atlenc.h>  	FILE* fp; 	fopen_s(&fp, "h:\\123.jpg", "rb"); 	fseek(fp, 0, SEEK_END); 	UINT FileSize = ftell(fp); 	PVOID pbuf = malloc(FileSize); 	fseek(fp, 0, SEEK_SET); 	fread(pbuf, FileSize, 1, fp); 	fclose(fp);  	int Base64EncLen = Base64EncodeGetRequiredLength(FileSize, ATL_BASE64_FLAG_NOCRLF); 	PCHAR szEncBase64 = (PCHAR)malloc(Base64EncLen); 	Base64Encode((PBYTE)pbuf, FileSize, szEncBase64, &Base64EncLen, ATL_BASE64_FLAG_NOCRLF);  	int Base64DecLen = Base64DecodeGetRequiredLength(Base64EncLen); 	PVOID pDebuf = malloc(Base64DecLen); 	Base64Decode(szEncBase64, Base64EncLen, (PBYTE)pDebuf, &Base64DecLen);  	FILE* file; 	fopen_s(&file, "d:\\1.jpg", "wb"); 	fwrite(pDebuf, Base64DecLen, 1, file); 	fclose(file);  	free(pbuf); 	free(szEncBase64); 	free(pDebuf); 中间的代码是放在main里运行吗？为什么我一用就报错啊，点开错误信息出了atlalloc.h和atlconv.h这两个头文件，显示了一大堆"应输入声明"。另外这种base64编码解码的原理是什么啊？有没有什么可供参考的资料啊。

user: user-1
content: #include <atlenc.h>  	FILE* fp; 	fopen_s(&fp, "h:\\123.jpg", "rb"); 	fseek(fp, 0, SEEK_END); 	UINT FileSize = ftell(fp); 	PVOID pbuf = malloc(FileSize); 	fseek(fp, 0, SEEK_SET); 	fread(pbuf, FileSize, 1, fp); 	fclose(fp);  	int Base64EncLen = Base64EncodeGetRequiredLength(FileSize, ATL_BASE64_FLAG_NOCRLF); 	PCHAR szEncBase64 = (PCHAR)malloc(Base64EncLen); 	Base64Encode((PBYTE)pbuf, FileSize, szEncBase64, &Base64EncLen, ATL_BASE64_FLAG_NOCRLF);  	int Base64DecLen = Base64DecodeGetRequiredLength(Base64EncLen); 	PVOID pDebuf = malloc(Base64DecLen); 	Base64Decode(szEncBase64, Base64EncLen, (PBYTE)pDebuf, &Base64DecLen);  	FILE* file; 	fopen_s(&file, "d:\\1.jpg", "wb"); 	fwrite(pDebuf, Base64DecLen, 1, file); 	fclose(file);  	free(pbuf); 	free(szEncBase64); 	free(pDebuf);

user: user-2
content: 仅供参考 和 拿去用吧 不是一回事！

user: user-0
content: 引用 3 楼 赵4老师的回复:仅供参考：#include <stdio.h> #include <string.h> #include <stdlib.h>  #define BASE64_VALUE_SZ 256 int     base64_value[BASE64_VALUE_SZ]; const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; class Base64Utility { public:     Base64Utility();     int base64_encode(char *src, int srclen, char *dst, int tail);     int base64_decode(char *src, int srclen, char *dst); private:     void base_64_init(void); }; Base64Utility::Base64Utility() {     base_64_init(); } void Base64Utility::base_64_init(void) {     int i;      for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;     for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;     base64_value['='] = 0; } int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {     int     bits, char_count, len;     char    *o_char, *lim, *o_lim;     unsigned char   c;      if ( !src || !dst) return 0;     len = srclen;     lim = src + len;     o_char = dst;     o_lim  = dst + (len*4)/3 + 1;     char_count = 0;     bits = 0;     while ( (src < lim) && (o_char < o_lim)) {         c = *(src++);         bits += c;         char_count++;         if (char_count == 3) {             *(o_char++) = alphabet[bits >> 18];             *(o_char++) = alphabet[(bits >> 12) & 0x3f];             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             *(o_char++) = alphabet[bits & 0x3f];             bits = 0;             char_count = 0;         } else {             bits <<= 8;         }     }     if (char_count != 0) {         bits <<= 16 - (8 * char_count);         *(o_char++) = alphabet[bits >> 18];         *(o_char++) = alphabet[(bits >> 12) & 0x3f];         if (char_count == 1) {             if (tail) {                 *(o_char++) = '=';                 *(o_char++) = '=';             }         } else {             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             if (tail) {                 *(o_char++) = '=';             }         }     }     *(o_char) = 0;     return strlen(dst); } int Base64Utility::base64_decode(char *src, int srclen, char *dst) {     int j;     unsigned int k;     int c, base_result_sz;     long val;      if (!src || !dst) return 0;     base_result_sz = srclen;     val = c = 0;     for (j = 0; *src; src++) {         k = (int) *src % BASE64_VALUE_SZ;         if (base64_value[k] < 0) continue;         val <<= 6;         val += base64_value[k];         if (++c < 4) continue;         dst[j++] = (char) (val >> 16);         dst[j++] = (val >> 8) & 0xff;         dst[j++] = val & 0xff;         val = c = 0;     }     switch (c) {         case 2://xxxxxx xx0000             dst[j++] = (val >> 4) & 0xff;         break;         case 3://XXXXXX XXxxxx xxxx00             dst[j++] = (char) (val >> 10);             dst[j++] = (val >> 2) & 0xff;         break;     }     return j; } Base64Utility b64u; #define MAXLENS 1024768 #define MAXLEND 1366360 char bufd[MAXLEND]; char bufs[MAXLENS]; FILE *fs,*fd; int fsize; int main(int argc,char *argv[]) {     if (argc<4) {     USE:         printf("%s <-e|-E|-d> srcfile desfile\n",argv[0]);         return 1;     }     if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;     if (0==stricmp(argv[1],"-e")) {         fs=fopen(argv[2],"rb");         if (NULL==fs) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufs,1,MAXLENS,fs);         if (fsize<=0) {             fclose(fs);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);         fclose(fs);         b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));         fd=fopen(argv[3],"w");         if (NULL==fd) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         fprintf(fd,"%s",bufd);         fclose(fd);     } else {//0==stricmp(argv[1],"-d")         fd=fopen(argv[2],"rb");         if (NULL==fd) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufd,1,MAXLEND,fd);         if (fsize<=0) {             fclose(fd);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);         fclose(fd);         fsize=b64u.base64_decode(bufd,fsize,bufs);         fs=fopen(argv[3],"wb");         if (NULL==fs) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {             printf("Write %s error!\n",argv[3]);             fclose(fs);             return 5;         }         fclose(fs);     }     return 0; }  这是将解码的数据输出到一个文件里了吗？经过测试了吗？

user: user-2
content: 再供参考：#pragma comment(lib,"crypt32") #include <windows.h> #include <stdio.h> #include <tchar.h>  //+------------------------------------------------------------------------- // convert formatted string to binary // If cchString is 0, then pszString is NULL terminated and // cchString is obtained via strlen() + 1. // dwFlags defines string format // if pbBinary is NULL, *pcbBinary returns the size of required memory // *pdwSkip returns the character count of skipped strings, optional // *pdwFlags returns the actual format used in the conversion, optional //-------------------------------------------------------------------------- BOOL WINAPI CryptStringToBinaryA(     IN     LPCSTR  pszString,     IN     DWORD     cchString,     IN     DWORD     dwFlags,     IN     BYTE     *pbBinary,     IN OUT DWORD    *pcbBinary,     OUT    DWORD    *pdwSkip,    //OPTIONAL     OUT    DWORD    *pdwFlags    //OPTIONAL     ); //+------------------------------------------------------------------------- // convert formatted string to binary // If cchString is 0, then pszString is NULL terminated and // cchString is obtained via strlen() + 1. // dwFlags defines string format // if pbBinary is NULL, *pcbBinary returns the size of required memory // *pdwSkip returns the character count of skipped strings, optional // *pdwFlags returns the actual format used in the conversion, optional //-------------------------------------------------------------------------- BOOL WINAPI CryptStringToBinaryW(     IN     LPCWSTR  pszString,     IN     DWORD     cchString,     IN     DWORD     dwFlags,     IN     BYTE     *pbBinary,     IN OUT DWORD    *pcbBinary,     OUT    DWORD    *pdwSkip,    //OPTIONAL     OUT    DWORD    *pdwFlags    //OPTIONAL     ); #ifdef UNICODE #define CryptStringToBinary  CryptStringToBinaryW #else #define CryptStringToBinary  CryptStringToBinaryA #endif // !UNICODE  //+------------------------------------------------------------------------- // convert binary to formatted string // dwFlags defines string format // if pszString is NULL, *pcchString returns the size of required memory in byte //-------------------------------------------------------------------------- BOOL WINAPI CryptBinaryToStringA(     IN     CONST BYTE  *pbBinary,     IN     DWORD        cbBinary,     IN     DWORD        dwFlags,     IN     LPSTR      pszString,     IN OUT DWORD       *pcchString     ); //+------------------------------------------------------------------------- // convert binary to formatted string // dwFlags defines string format // if pszString is NULL, *pcchString returns the size of required memory in byte //-------------------------------------------------------------------------- BOOL WINAPI CryptBinaryToStringW(     IN     CONST BYTE  *pbBinary,     IN     DWORD        cbBinary,     IN     DWORD        dwFlags,     IN     LPWSTR      pszString,     IN OUT DWORD       *pcchString     ); #ifdef UNICODE #define CryptBinaryToString  CryptBinaryToStringW #else #define CryptBinaryToString  CryptBinaryToStringA #endif // !UNICODE  // dwFlags has the following defines #define CRYPT_STRING_BASE64HEADER           0x00000000 #define CRYPT_STRING_BASE64                 0x00000001 #define CRYPT_STRING_BINARY                 0x00000002 #define CRYPT_STRING_BASE64REQUESTHEADER    0x00000003 #define CRYPT_STRING_HEX                    0x00000004 #define CRYPT_STRING_HEXASCII               0x00000005 #define CRYPT_STRING_BASE64_ANY             0x00000006 #define CRYPT_STRING_ANY                    0x00000007 #define CRYPT_STRING_HEX_ANY                0x00000008 #define CRYPT_STRING_BASE64X509CRLHEADER    0x00000009 #define CRYPT_STRING_HEXADDR                0x0000000a #define CRYPT_STRING_HEXASCIIADDR           0x0000000b  #define CRYPT_STRING_NOCR                   0x80000000  // CryptBinaryToString uses the following flags // CRYPT_STRING_BASE64HEADER - base64 format with certificate begin //                             and end headers // CRYPT_STRING_BASE64 - only base64 without headers // CRYPT_STRING_BINARY - pure binary copy // CRYPT_STRING_BASE64REQUESTHEADER - base64 format with request begin //                                    and end headers // CRYPT_STRING_BASE64X509CRLHEADER - base64 format with x509 crl begin //                                    and end headers // CRYPT_STRING_HEX - only hex format // CRYPT_STRING_HEXASCII - hex format with ascii char display // CRYPT_STRING_HEXADDR - hex format with address display // CRYPT_STRING_HEXASCIIADDR - hex format with ascii char and address display // // CryptBinaryToString accepts CRYPT_STRING_NOCR or'd into one of the above. // When set, line breaks contain only LF, instead of CR-LF pairs.  // CryptStringToBinary uses the following flags // CRYPT_STRING_BASE64_ANY tries the following, in order: //    CRYPT_STRING_BASE64HEADER //    CRYPT_STRING_BASE64 // CRYPT_STRING_ANY tries the following, in order: //    CRYPT_STRING_BASE64_ANY //    CRYPT_STRING_BINARY -- should always succeed // CRYPT_STRING_HEX_ANY tries the following, in order: //    CRYPT_STRING_HEXADDR //    CRYPT_STRING_HEXASCIIADDR //    CRYPT_STRING_HEXASCII //    CRYPT_STRING_HEX   char *flags[12]={ "CRYPT_STRING_BASE64HEADER", "CRYPT_STRING_BASE64", "CRYPT_STRING_BINARY", "CRYPT_STRING_BASE64REQUESTHEADER", "CRYPT_STRING_HEX", "CRYPT_STRING_HEXASCII", "CRYPT_STRING_BASE64_ANY", "CRYPT_STRING_ANY", "CRYPT_STRING_HEX_ANY", "CRYPT_STRING_BASE64X509CRLHEADER", "CRYPT_STRING_HEXADDR", "CRYPT_STRING_HEXASCIIADDR", }; #define MAXC 1024 BYTE b[22]={ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, 0x41,0x42,0xB0,0xA1,0x4A,0x55, }; BOOL r; DWORD len,dwFlags; TCHAR s[MAXC]; int _tmain() {     _tprintf(_T("API CryptBinaryToString in crypt32.dll Demonstration:\n"));     for (dwFlags=0;dwFlags<12;dwFlags++) {         if (dwFlags==2          || dwFlags==6          || dwFlags==7          || dwFlags==8) continue;         r=CryptBinaryToString(b,22,dwFlags,NULL,&len);         if (!r) {             _tprintf(_T("CryptBinaryToString error!\n"));             return 1;         }         if (len>MAXC) {             _tprintf(_T("%d==len>MAXC==%d!\n"),len,MAXC);             return 2;         }         r=CryptBinaryToString(b,22,dwFlags,s,&len);         if (!r) {             _tprintf(_T("CryptBinaryToString error!\n"));             return 3;         }         _tprintf(_T("\n%s:[\n%s]\n"),flags[dwFlags],s);     }     return 0; } //API CryptBinaryToString in crypt32.dll Demonstration: // //CRYPT_STRING_BASE64HEADER:[ //-----BEGIN CERTIFICATE----- //AAECAwQFBgcICQoLDA0OD0FCsKFKVQ== //-----END CERTIFICATE----- //] // //CRYPT_STRING_BASE64:[ //AAECAwQFBgcICQoLDA0OD0FCsKFKVQ== //] // //CRYPT_STRING_BASE64REQUESTHEADER:[ //-----BEGIN NEW CERTIFICATE REQUEST----- //AAECAwQFBgcICQoLDA0OD0FCsKFKVQ== //-----END NEW CERTIFICATE REQUEST----- //] // //CRYPT_STRING_HEX:[ //        00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f //        41 42 b0 a1 4a 55 //] // //CRYPT_STRING_HEXASCII:[ //        00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f   ................ //        41 42 b0 a1 4a 55                                  AB..JU //] // //CRYPT_STRING_BASE64X509CRLHEADER:[ //-----BEGIN X509 CRL----- //AAECAwQFBgcICQoLDA0OD0FCsKFKVQ== //-----END X509 CRL----- //] // //CRYPT_STRING_HEXADDR:[ //0000    00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f //0010    41 42 b0 a1 4a 55 //] // //CRYPT_STRING_HEXASCIIADDR:[ //0000    00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f   ................ //0010    41 42 b0 a1 4a 55                                  AB..JU //] //

user: user-2
content: 仅供参考：#include <stdio.h> #include <string.h> #include <stdlib.h>  #define BASE64_VALUE_SZ 256 int     base64_value[BASE64_VALUE_SZ]; const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; class Base64Utility { public:     Base64Utility();     int base64_encode(char *src, int srclen, char *dst, int tail);     int base64_decode(char *src, int srclen, char *dst); private:     void base_64_init(void); }; Base64Utility::Base64Utility() {     base_64_init(); } void Base64Utility::base_64_init(void) {     int i;      for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;     for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;     base64_value['='] = 0; } int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {     int     bits, char_count, len;     char    *o_char, *lim, *o_lim;     unsigned char   c;      if ( !src || !dst) return 0;     len = srclen;     lim = src + len;     o_char = dst;     o_lim  = dst + (len*4)/3 + 1;     char_count = 0;     bits = 0;     while ( (src < lim) && (o_char < o_lim)) {         c = *(src++);         bits += c;         char_count++;         if (char_count == 3) {             *(o_char++) = alphabet[bits >> 18];             *(o_char++) = alphabet[(bits >> 12) & 0x3f];             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             *(o_char++) = alphabet[bits & 0x3f];             bits = 0;             char_count = 0;         } else {             bits <<= 8;         }     }     if (char_count != 0) {         bits <<= 16 - (8 * char_count);         *(o_char++) = alphabet[bits >> 18];         *(o_char++) = alphabet[(bits >> 12) & 0x3f];         if (char_count == 1) {             if (tail) {                 *(o_char++) = '=';                 *(o_char++) = '=';             }         } else {             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             if (tail) {                 *(o_char++) = '=';             }         }     }     *(o_char) = 0;     return strlen(dst); } int Base64Utility::base64_decode(char *src, int srclen, char *dst) {     int j;     unsigned int k;     int c, base_result_sz;     long val;      if (!src || !dst) return 0;     base_result_sz = srclen;     val = c = 0;     for (j = 0; *src; src++) {         k = (int) *src % BASE64_VALUE_SZ;         if (base64_value[k] < 0) continue;         val <<= 6;         val += base64_value[k];         if (++c < 4) continue;         dst[j++] = (char) (val >> 16);         dst[j++] = (val >> 8) & 0xff;         dst[j++] = val & 0xff;         val = c = 0;     }     switch (c) {         case 2://xxxxxx xx0000             dst[j++] = (val >> 4) & 0xff;         break;         case 3://XXXXXX XXxxxx xxxx00             dst[j++] = (char) (val >> 10);             dst[j++] = (val >> 2) & 0xff;         break;     }     return j; } Base64Utility b64u; #define MAXLENS 1024768 #define MAXLEND 1366360 char bufd[MAXLEND]; char bufs[MAXLENS]; FILE *fs,*fd; int fsize; int main(int argc,char *argv[]) {     if (argc<4) {     USE:         printf("%s <-e|-E|-d> srcfile desfile\n",argv[0]);         return 1;     }     if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;     if (0==stricmp(argv[1],"-e")) {         fs=fopen(argv[2],"rb");         if (NULL==fs) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufs,1,MAXLENS,fs);         if (fsize<=0) {             fclose(fs);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);         fclose(fs);         b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));         fd=fopen(argv[3],"w");         if (NULL==fd) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         fprintf(fd,"%s",bufd);         fclose(fd);     } else {//0==stricmp(argv[1],"-d")         fd=fopen(argv[2],"rb");         if (NULL==fd) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufd,1,MAXLEND,fd);         if (fsize<=0) {             fclose(fd);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);         fclose(fd);         fsize=b64u.base64_decode(bufd,fsize,bufs);         fs=fopen(argv[3],"wb");         if (NULL==fs) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {             printf("Write %s error!\n",argv[3]);             fclose(fs);             return 5;         }         fclose(fs);     }     return 0; }

user: user-0
content: 这是程序最后运行的结果

user: user-0
content: 现在需要对一个图片编码后的字符串进行解码，这个图片编码后的字符串我存在一个txt文件中，然后在程序中读取这个txt文件，用一个长度为30000的字符数组来存放它，这个字符串差不多有两万多位，在网上找了一个base64的解码方法，代码如下： 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; } 现在的问题是，我用这个函数对字符串进行解码后，显示的解码后的字符串的长度才11，解码后的源码也就是几个乱码，想问一下这是什么问题？是解码函数的问题还是什么？如果是解码函数的问题能提供一个经过测试的解码函数吗？在这里先谢了。 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);