Many netizens have made a detailed introduction to the encoding and decoding rules of UTF16 and base64, so I will not go into details here.
This article is also written by referring to the articles of many netizens. Here, we dedicate our writing to the people who need it. The original authors of Some codes have no idea,
Therefore, the source cannot be specified.
# Include <stdio. h>
# Include <stdlib. h>
# Define encode
Static const char cb64 [] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
Static void encodeblock (unsigned char in [3], unsigned char out [4], int Len ){
Out [0] = cb64 [in [0]> 2];
Out [1] = cb64 [(in [0] & 0x03) <4) | (in [1] & 0xf0)> 4)];
Out [2] = (unsigned char) (LEN> 1? Cb64 [(in [1] & 0x0f) <2) | (in [2] & 0xc0)> 6)]: '= ');
Out [3] = (unsigned char) (LEN> 2? Cb64 [in [2] & 0x3f]: '= ');
}
Static void base64_encode (File * infile, file * OUTFILE ){
Unsigned char in [3], out [4];
Int I, Len;
While (! Feof (infile )){
Len = 0;
For (I = 0; I <3; I ++ ){
In [I] = (unsigned char) GETC (infile );
If (! Feof (infile )){
Len ++;
} Else {
In [I] = 0;
}
}
If (LEN ){
Encodeblock (In, out, Len );
For (I = 0; I <4; I ++ ){
Putc (out [I], OUTFILE );
}
}
}
}
Int main (){
# Ifdef encode
File * normal16 = fopen ("utf16_encode_file", "rb"); // utf16_encode_file is a file encoded with UTF16
File * normal64 = fopen ("base64_encode_file", "WB"); // base64_encode_file is the base64 encoded target file.
If (normal16 = NULL ){
Fprintf (stderr, "failed to open input file \ n ");
Exit (0 );
}
Base64_encode (normal16, normal64 );
Fclose (normal16 );
Fclose (normal64 );
# Endif
Return 0;
}
PS: I wrote an article at the beginning. Please forgive me. If you have any questions or communication, you can add your YY: 301558660
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/7723075