Home > Back-end >  How much is the length of the base64 decoding?
How much is the length of the base64 decoding?

Time:05-25

Base64 encoding length is ceil (n/3) * 4 n is length of the original data
The length of the base64 decoding how to calculate?

CodePudding user response:

Base64 existence=number, so just calculate a range, 3/4 length * + 1 maximum length, if you want to specific calculate the length of the code before, need to use their judgment to fill=size minus the corresponding placeholder

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
# include & lt; String. H>
#include

# define BASE64_VALUE_SZ 256
Int base64_value [BASE64_VALUE_SZ];
Const unsigned char alphabet [65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/";
The 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 & lt; BASE64_VALUE_SZ; I++) base64_value [I]=1;
For (I=0; I & lt; 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;
Lim o_char char * and * and * o_lim;
Unsigned char c;

If (! The 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 & lt; Lim) & amp; & (o_char & lt; O_lim)) {
C=* (src++);
Bits +=c;
Char_count + +;
If (char_count==3) {
* (o_char + +)=alphabet [18] bits & gt; & gt;;
* (o_char + +)=alphabet [(bits & gt; & gt; 12) & amp; 0 x3f];
* (o_char + +)=alphabet [(bits & gt; & gt; 6) & amp; 0 x3f];
* (o_char + +)=alphabet [bits & amp; 0 x3f];
Bits=0;
Char_count=0;
} else {
Bits & lt; <=8;
}
}
If (char_count!=0) {
Bits & lt; <=16 - (8 * char_count);
* (o_char + +)=alphabet [18] bits & gt; & gt;;
* (o_char + +)=alphabet [(bits & gt; & gt; 12) & amp; 0 x3f];
If (char_count==1) {
If (tail) {
* (o_char + +)==' ';
* (o_char + +)==' ';
}
} else {
* (o_char + +)=alphabet [(bits & gt; & gt; 6) & amp; 0 x3f];
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 (! The 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 & lt; <=6;
Val +=base64_value [k].
If (c + + & lt; 4) continue;
DST [j++]=(char) (val & gt;> 16);
DST [j++]=(val & gt;> 8) & amp; 0 XFF.
DST [j++]=val & amp; 0 XFF.
Val=c=0;
}
The switch (c) {
Case 2://XXXXXX xx0000
DST [j++]=(val & gt;> 4) & amp; 0 XFF.
break;
Case 3://XXXXXX XXXXXX xxxx00
DST [j++]=(char) (val & gt;> 10);
DST [j++]=(val & gt;> 2) & amp; 0 XFF.
break;
}
Return j;
}
Base64Utility b64u;
# define MAXLENS 1024768
# define MAXLEND 1366360
Char bufd [MAXLEND];
Char bufs [MAXLENS];
Fs FILE * and * fd;
Int fsize;
Int main (int arg c, char * argv []) {
If (argc<4) {
USE:
Printf (" % s & lt; The -e | -e | - d> Srcfile desfile \ n ", argv [0]);
return 1;
}
If (stricmp (argv [1], the "-e") & amp; & Stricmp (argv [1], "3-d")) goto the USE;
If (0==stricmp (argv [1], the "-e")) {
The fs=fopen (argv [2], "rb");
If (NULL==fs) {
Printf (" Can not open the file % s! \ n ", argv [2]);
Return 2;
}
Fsize=fread (bufs, 1, MAXLENS, fs);
If (fsize<=0) {
The fclose (fs);
Printf (" Can not read the file % s! \ n ", argv [2]);
Return 3;
}
If (MAXLENS==fsize) printf (" Warning: Up to % d bytes. \ n ", MAXLENS);
The fclose (fs);
B64u. Base64_encode (bufs, fsize bufd, (=='E' argv [2] [1]));
Fd=fopen (argv [3], "w");
If (NULL==fd) {
Printf (" Can not create the file % s! \ n ", argv [3]);
The return of 4;
}
Fprintf (fd, "% s", bufd);
The fclose (fd);
} else {//0==stricmp (argv [1], "3-d")
Fd=fopen (argv [2], "rb");
If (NULL==fd) {
Printf (" Can not open the file % s! \ n ", argv [2]);
Return 2;
}
Fsize=fread (bufd, 1, MAXLEND, fd);
If (fsize<=0) {
The fclose (fd);
Printf (" Can not read the file % s! \ n ", argv [2]);
Return 3;
}
If (MAXLEND==fsize) printf (" Warning: Up to % d bytes. \ n ", MAXLEND);
The fclose (fd);
Fsize=b64u. Base64_decode (bufd, fsize, bufs);
The fs=fopen (argv [3], "wb");
If (NULL==fs) {
Printf (" Can not create the file % s! \ n ", argv [3]);
The return of 4;
}
If (fsize!=(int) fwrite (bufs, 1, fsize, fs)) {
Printf (" Write % s error! \ n ", argv [3]);
The fclose (fs);
Return 5;
}
The fclose (fs);
}
return 0;
}
nullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related