Java Encode and Decode Hex String using Apache Commons Codec
Tags: Apache Commons Codec Java Hex Java Encode Hex Java Decode Hex
In this Java tutorial we learn how to use Hex class of Apache Commons Codec library to encode byte[] array to hexadecimal String and decode hexadecimal String to byte[] array.
Table of contents
- Add Apache Commons Codec Dependency to Java project
- Convert Byte Array to Hex String in Java
- Convert Hex String to Byte Array in Java
- Convert SHA-1 Hashed Byte Array to Hex String
Add Apache Commons Codec Dependency to Java project
To use Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.
implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'
To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
To have more information about the Apache Commons Codec Java library you can visit the project home page at commons.apache.org/proper/commons-codec/
Convert Byte Array to Hex String in Java
With Apache Commons Codec we can use the Hex.encodeHexString() method to convert an byte[] array into hexadecimal string as following Java program.
HexExample1.java
import org.apache.commons.codec.binary.Hex;
public class HexExample1 {
public static void main(String... args) {
byte[] inputBytes = "SimpleSolution".getBytes();
String hexString = Hex.encodeHexString(inputBytes);
System.out.println(hexString);
}
}
53696d706c65536f6c7574696f6e
Convert Hex String to Byte Array in Java
We also can decode a hexadecimal String to byte[] array using the Hex.decodeHex() as below.
HexExample2.java
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import java.util.Arrays;
public class HexExample2 {
public static void main(String... args) throws DecoderException {
String hexString = "53696d706c65536f6c7574696f6e";
byte[] outputBytes = Hex.decodeHex(hexString);
System.out.println(Arrays.toString(outputBytes));
System.out.println(new String(outputBytes));
}
}
[83, 105, 109, 112, 108, 101, 83, 111, 108, 117, 116, 105, 111, 110]
SimpleSolution
Convert SHA-1 Hashed Byte Array to Hex String
In the following Java program we show how to use Hex.encodeHexString() to convert SHA-1 hash result from byte[] array to hexadecimal String.
HexExample3.java
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class HexExample3 {
public static void main(String... args) {
String inputData = "Simple Solution";
MessageDigest sha1MessageDigest = DigestUtils.getSha1Digest();
byte[] hashedBytes = sha1MessageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
String hashedString = Hex.encodeHexString(hashedBytes);
System.out.println("Input String:" + inputData);
System.out.println("SHA-1:" + hashedString);
}
}
Input String:Simple Solution
SHA-1:23921d0724f0388c797b1383c39a6eaea5c134e6
Happy Coding 😊