C # Base64 Coding _ Practical Tips

Source: Internet
Author: User
Tags base64 readline
I. Coding rules for BASE64
The idea of BASE64 encoding is to encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into a byte array. A group of 3 bytes. Arrange the 24-bit data in order, then divide the 24-bit data into 4 groups, that is, 6 bits per group. Then fill a byte with two 0 in front of each group's top digit. This encodes a 3-byte-A-group of data back into 4 bytes. When the number of bytes of data to be encoded is not an integral multiple of 3, that is, the last group is not 3 bytes when grouped. This is populated with 1 to 2 0 bytes in the last group. and add 1 to 2 "=" At the end after the final encoding is complete.

Example: BASE64 encoding of ABC will be performed:
1, first take the corresponding ASCII value of ABC. (A) B (C) (67);
2, then take the binary value A (01000001) B (01000010) C (01000011);
3, then the three bytes of the binary code to connect (010000010100001001000011);
4, then 6-bit units into 4 blocks, and at the highest level of two 0 after the formation of 4 bytes encoded values, (00010000) (00010100) (00001001) (00000011), where the blue part of the real data;
5. Convert these four bytes of data into 10 (16) (20) (9) (3);
6. Finally, according to the 64 basic character tables given by BASE64, find the corresponding ASCII code character (Q) (U) (J) (D), where the value is actually the index of the data in the character table.

Note: BASE64 character chart: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/

two. Decoding rules
The decoding process is to restore 4 bytes to 3 bytes and then rearrange the byte array into data according to different data forms.

Three. Implementation in C #
Copy Code code as follows:

byte[] bytes = Encoding.Default.GetBytes ("HelloWorld");
String str = convert.tobase64string (bytes);
Console.WriteLine (str);
Console.ReadLine ();
Base decode
bytes = convert.frombase64string (str);
Console.WriteLine (Encoding.Default.GetString (bytes));
Console.ReadLine ();

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.