org.apache.commons.codec.binary.Hex Example

Q

What is org.apache.commons.codec.binary.Hex class? How to use org.apache.commons.codec.binary.Hex class?

✍: FYIcenter.com

A

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

 

Using commons-codec.jar in Java Programs

⇒⇒FAQ for Apache commons-codec.jar

2017-04-22, 371👍, 0💬