How to convert Byte Array in Hex String in Java

  1. HEX_ARRAY[] method to convert byte array to hex string in Java
  2. Hex.encodeHexString() method to convert byte array to hex string in Java
  3. DatatypeConverter() method to convert byte Array in hex String in Java
  4. append(.format) method for conversion of byte array into hex string in Java

In this tutorial, we will learn how to convert byte array to hex string in Java.

HEX_ARRAY[] method to convert byte array to hex string in Java

The first method we will be starting with, for this conversion we will use HEX_ARRAY[]consisting of all possible hex values. This method is faster than any other alternative.

Example Codes:

public class SimpleTesting {
    char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    byte b[]= new byte[2];
    b[0] = 20;
    b[1] = 10;
    char[] hexChars = new char[b.length * 2];
    for (int j = 0; j < b.length; j++) {
        int v = b[j] & 0xFF;
        hexChars[j * 2] = HEX_ARRAY[v >>> 4];
        hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
    }
        System.out.println(hexChars);
 }
}

Output:

140A

Hex.encodeHexString() method to convert byte array to hex string in Java

The second method to convert byte array to hex string in Java is Hex.encodeHexString() method. Since its an apache’s commons library, hence method apache.commons.codec.binary.Hex() needs to be imported first inside the compiler.

Example Codes:

import java.util.Arrays;
import org.apache.commons.codec.binary.Hex;

public class SimpleTesting {
    public static void main(String[] args) {
        byte[] byteArray = new byte[]
        {'S', 'i', 'm', 'p', 'l', 'e', 'T', 'e', 's',
        't', 'i', 'n', 'g'};
        System.out.println("Byte Array: "); 
        System.out.println(Arrays.toString(byteArray));
        System.out.println("Hex String Conversion: " +
                          Hex.encodeHexString(byteArray));
    }
}

Output:

Byte Array: Simple Testing
Hex String Conversion: 53696d706c652054657374696e67

DatatypeConverter() method to convert byte Array in hex String in Java

Another approach is javax.xml.bind.DatatypeConverter.printHexBinary() method in Java. This method takes the bytes and converts it into Hex from an array.

Example Codes:

import java.util.Arrays;

public class SimpleTesting {
    public static void main(String[] args) {
       byte byteArray[] = 
       {(byte)00, (byte)10, (byte)20, (byte)30, (byte)40};
       String hexString = 
           javax.xml.bind.DatatypeConverter
           .printHexBinary(byteArray);
       System.out.println("Byte Array: "); 
       System.out.println(Arrays.toString(byteArray));
       System.out.println("Hex String Conversion: "
                          + hexString);
    }
}

Output:

Byte Array: 
[0, 10, 20, 30, 40]
Hex String Conversion: 000A141E28

append(.format) method for conversion of byte array into hex string in Java

Another method that can be used is to include append(.format) using stringBuilder in Java. It works by converting every single value from byte array and convert one by one to hex string accordingly.

Example Codes:

public class SimpleTesting {
    public static void main(String[] args) {
        byte a[]= new byte[2];
        a[0] = 20;
        a[1] = 10;
        StringBuilder sb = new StringBuilder(a.length * 2);
        for(byte b: a)
            sb.append(String.format("%02x", b));
        System.out.println(sb); 
    }
}

Output:

140a

Related Article - Java Array

  • How to concatenate two arrays in Java
  • Related Article - Java String

  • How to concatenate two arrays in Java
  • comments powered by Disqus