Jakarta-common-codec Use Notes

Source: Internet
Author: User
Tags base64

Commons codec provides Base64, Hex, and Metaphone, Soundex and other coding algorithms.

Download Address: http://commons.apache.org/codec/

A.BASE64 Coding and decoding

package demo;

import org.apache.commons.codec.binary.Base64;

public class Base64Test ...{

  public static void main(String[] args) ...{

    Base64 base64 = new Base64();
    String str = "中文";
    byte[] enbytes = null;
    String encodeStr = null;
    byte[] debytes = null;
    String decodeStr = null;

    enbytes = base64.encode(str.getBytes());
    encodeStr = new String(enbytes);
    debytes = base64.decode(enbytes);
    decodeStr = new String(debytes);

    System.out.println("编码前:" + str);
    System.out.println("编码后:" + encodeStr);
    System.out.println("解码后:" + decodeStr);
  }
}

B.hex Coding and decoding

package demo;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class HexTest ...{

  public static void main(String[] args) throws DecoderException ...{

    Hex hex = new Hex();
    String str = "中文";
    char[] enbytes = null;
    String encodeStr = null;
    byte[] debytes = null;
    String decodeStr = null;

    enbytes = hex.encodeHex(str.getBytes());
    encodeStr = new String(enbytes);
    debytes = hex.decodeHex(enbytes);
    decodeStr = new String(debytes);

    System.out.println("编码前:" + str);
    System.out.println("编码后:" + encodeStr);
    System.out.println("解码后:" + decodeStr);
  }
}

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.