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, 3044🔥, 0💬
Popular Posts:
ANTLR is a powerful parser generator for multiple programming languages including Java. ANTLR contai...
JDK 17 jdk.hotspot.agent.jmod is the JMOD file for JDK 17 Hotspot Agent module. JDK 17 Hotspot Agent...
JDK 11 jdk.internal.vm.ci.jmod is the JMOD file for JDK 11 Internal VM CI module. JDK 11 Internal VM...
How to show the XML parsing flow with sax\DocumentTracer.java provided in the Apache Xerces package?...
JRE 5 sunjce_provider.jar is the JAR file for JRE 5 Sun JCE Provider, which provides implementations...