Convert UTF16 encoding to base64 encoding (1)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.