Java Implementation BASE64 codec

Source: Internet
Author: User
Tags base64

Java Implementation BASE64 codec

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

BASE64 and other similar coding algorithms are often used to convert binary data into textual data, which is intended to simplify storage or transmission. In more detail, the BASE64 algorithm is primarily used to convert binary data into ASCII string formats. The Java language provides a good implementation of the BASE64 algorithm, the Apache Commons codec toolkit. This article will briefly describe how to use BASE64 and how it works.

Here we encode the string using BASE64:

Import Java.io.unsupportedencodingexception;import Org.apache.commons.codec.binary.base64;public class Base64Test { public static void Main (string[] args) {String str = ' Hello world '; try{byte[] encodeBase64 = base64.encodebase64 (str.getby TES ("UTF-8")); System.out.println ("RESULT:" + new String (encodeBase64));} catch (Unsupportedencodingexception e) {e.printstacktrace ();}}}

The output is:

result:sgvsbg8gv29ybgq=

The above output string is a 8-bit binary value of the "Hello World" string that is concatenated and then grouped in 6 bits. Each group is then converted to a separate number and mapped to the index of the Base64.

Binarydec Base64010010  S000110  6   G010101  V101100  b000110  6   G111100  8100000  g010101  d110110  2111101  9110010  y011011  b000110  6   G010000  Q

Note: The string is finally prefixed with "=", which means the end of the string encoding.





Java Implementation BASE64 codec

Related Article

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.