Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
org.apache.commons.codec.binary.Hex Example
What is org.apache.commons.codec.binary.Hex class? How to use org.apache.commons.codec.binary.Hex class?
✍: FYIcenter.com
org.apache.commons.codec.binary.Hex class is a Java class
offered in commons-codec.jar that
converts between byte arrays and strings hexadecimal digits.
Two commonly used static methods in org.apache.commons.codec.binary.Hex class are:
1. The encoding method:
public static char[] encodeHex(byte[] data) Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. The returned array will be double the length of the passed array, as it takes two characters to represent any given byte. Parameters: data - a byte[] to convert to Hex characters Returns: A char[] containing hexadecimal characters
2. The decoding method:
public static byte[] decodeHex(char[] data) throws DecoderException Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements. Parameters: data - An array of characters containing hexadecimal digits Returns: A byte array containing binary data decoded from the supplied char array.
Here is a simple example of org.apache.commons.codec.binary.Hex class:
// Copyright (c) 2016 FYIcenter.com import org.apache.commons.codec.binary.Hex; // Example of using the Hex class public class HexExample { public static void main(String[] args) throws Exception { System.out.println("encodeHex() Example:"); String inputString = "ABCD1234"; byte[] inputBytes = inputString.getBytes("UTF8"); char[] outputChars = Hex.encodeHex(inputBytes); String outputString = new String(outputChars); String expectedString = "4142434431323334"; System.out.println(" Input string: "+inputString); System.out.println(" Encoded string: "+outputString); System.out.println(" Expected string: "+expectedString); System.out.println("decodeHex() Example:"); inputString = "4142434431323334"; char[] inputChars = inputString.toCharArray(); byte[] outputBytes = Hex.decodeHex(inputChars); outputString = new String(outputBytes,"UTF8"); expectedString = "ABCD1234"; System.out.println(" Input string: "+inputString); System.out.println(" Encoded string: "+outputString); System.out.println(" Expected string: "+expectedString); } }
You can compile and run the above example in a command window as shown below:
C:\fyicenter>\local\jdk-1.8.0\bin\javac -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar HexExample.java C:\fyicenter>\local\jdk-1.8.0\bin\java -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar HexExample encodeHex() Example: Input string: ABCD1234 Encoded string: 4142434431323334 Expected string: 4142434431323334 decodeHex() Example: Input string: 4142434431323334 Encoded string: ABCD1234 Expected string: ABCD1234
⇒ org.apache.commons.codec.digest.DigestUtils Example
⇐ org.apache.commons.codec.binary.BinaryCodec Example
2017-04-22, 2980🔥, 0💬
Popular Posts:
JDK 6 tools.jar is the JAR file for JDK 6 tools. It contains Java classes to support different JDK t...
What Is log4j-1.2.15.jar? I got the JAR file from apache-log4j-1.2.15.zip. log4j-1.2.15.jar is the v...
jlGui is a music player for the Java platform. It is based on Java Sound 1.0 (i.e. JDK 1.3+). It sup...
What Is jaxb-impl-2.1.12.jar? Java Architecture for XML Binding (JAXB) is a Java API that allows Jav...
Old version of xml-apis.jar. JAR File Size and Download Location: File name: xmlParserAPIs.jar File ...