Encoding and decoding tool class sharing in various formats (Hex decoding base64 encoding) _java

Source: Internet
Author: User
Tags base64 unsupported hex code

Copy Code code as follows:

Import java.io.UnsupportedEncodingException;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;

Import org.apache.commons.codec.DecoderException;
Import org.apache.commons.codec.binary.Base64;
Import Org.apache.commons.codec.binary.Hex;
Import Org.apache.commons.lang.StringEscapeUtils;

/**
* Coding in various formats of the tool class.
*
* Integrated Commons-codec,commons-lang and JDK provided by the codec method.
*
*
*/
public class Encodeutils {

private static final String default_url_encoding = "UTF-8";

/**
* Hex code.
*/
/*public static String Hexencode (byte[] input) {
return hex.encodehexstring (input);
}*/

/**
* Hex decoding.
*/
public static byte[] Hexdecode (String input) {
try {
Return Hex.decodehex (Input.tochararray ());
catch (Decoderexception e) {
throw new IllegalStateException ("Hex Decoder exception", e);
}
}

/**
* Base64 code.
*/
public static String Base64Encode (byte[] input) {
return new String (base64.encodebase64 (input));
}

/**
* Base64 encoding, URL security (will Base64 the URL in the illegal characters,/= to other characters, see RFC3548).
*/
public static String Base64urlsafeencode (byte[] input) {
return base64.encodebase64urlsafestring (input);
}

/**
* Base64 decoding.
*/
public static byte[] Base64decode (String input) {
return base64.decodebase64 (input);
}

/**
* URL encoding, encode defaults to UTF-8.
*/
public static string UrlEncode (string input) {
try {
return Urlencoder.encode (input, default_url_encoding);
catch (Unsupportedencodingexception e) {
throw new IllegalArgumentException ("Unsupported Encoding Exception", e);
}
}

/**
* URL decoding, encode defaults to UTF-8.
*/
public static string UrlDecode (string input) {
try {
return Urldecoder.decode (input, default_url_encoding);
catch (Unsupportedencodingexception e) {
throw new IllegalArgumentException ("Unsupported Encoding Exception", e);
}
}

/**
* Html transcoding.
*/
public static string Htmlescape (string html) {
return stringescapeutils.escapehtml (HTML);
}

/**
* Html decoding.
*/
public static string Htmlunescape (String htmlescaped) {
Return stringescapeutils.unescapehtml (htmlescaped);
}

/**
* Xml transcoding.
*/
public static string Xmlescape (string xml) {
return Stringescapeutils.escapexml (XML);
}

/**
* XML decoding.
*/
public static string Xmlunescape (String xmlescaped) {
Return Stringescapeutils.unescapexml (xmlescaped);
}
}

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.