Convert Hexadecimal to ASCII String in Java

Hexa (six) plus decimal (ten) equals sixteen. It stands for the Base 16 Number System. Valid hexadecimal digits are numbers from 0 to 9 and letters from A through F. Hexadecimal numerals are widely used in computer science by computer system designers and programmers. In this example we show how to convert a String to Hexadecimal notation and convert Hexadecimal to ASCII string notation.

Convert ASCII to and from Hex

  • Using the String.format() method and pass a BigDecimal with the byte array of the original string, we can easily convert an ASCII string to Hexadecimal(Hex).
  • To convert Hexadecimal(Hex) to ASCII, we cut the Hex value in pairs, convert it to radix 16 using the Integer.parseInt(input, 16) method and cast it back to a char.
package com.memorynotfound;

import java.math.BigInteger;

public class ConvertHexAscii {

    static final String original = "memorynotfound.com";

    public static void main(String... args) {
        System.out.println("Original: " + original);
        String hex = toHexString(original);
        System.out.println("ASCII to Hex: " + hex);

        String ascii = fromHexString(hex);
        System.out.println("Hex to ASCII: " + ascii);
    }

    public static String toHexString(String input) {
        return String.format("%x", new BigInteger(1, input.getBytes()));
    }

    public static String fromHexString(String hex) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < hex.length(); i+=2) {
            str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
        }
        return str.toString();
    }

}

java.xml.bind.DatatypeConverter

We can also use the java.xml.bind.DatatypeConverter to easily convert an ASCII string to Hex and vice versa.

  • DatatypeConverter.printHexBinary(byte[] input) converts a byte array into hexadecimal format.
  • DatatypeConverter.parseHexBinary(String hex) converts hexadecimal string into a byte array. We can create an ASCII string using the new String(byte[] input) and optionally pass in a CharSet.
package com.memorynotfound;

import javax.xml.bind.DatatypeConverter;

public class ConvertHexAscii {

    static final String original = "memorynotfound.com";

    public static void main(String... args) {
        System.out.println("Original: " + original);
        String hex = DatatypeConverter.printHexBinary(original.getBytes());
        System.out.println("ASCII to Hex: " + hex.toLowerCase());

        byte[] bytes = DatatypeConverter.parseHexBinary(hex);
        String ascii = new String(bytes);
        System.out.println("Hex to ASCII: " + ascii);
    }

}

Apache Commons Codex

Usually Apache has a library for everything, no exception here. You can use the commons-codec library. This contains conversions for various codexes.

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>
  • Hex.encodeHex(byte[] input) converts a byte[] into a char[]. We can use the new String(char[] input) constructor to create a new ASCII string.
  • Hex.decodeHex(char[] input) converts the hexadecimal into a byte[]. We can use the new String(byte[] input) constructor to create a new ASCII string.
package com.memorynotfound;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class ConvertHexAscii {

    static final String original = "memorynotfound.com";

    public static void main(String... args) throws DecoderException {
        System.out.println("Original: " + original);
        char[] hexChar = Hex.encodeHex(original.getBytes());
        String hex = new String(hexChar);
        System.out.println("ASCII to Hex: " + hex);

        byte[] bytes = Hex.decodeHex(hexChar);
        String ascii = new String(bytes);
        System.out.println("Hex to ASCII: " + ascii);
    }
    
}

Output

Original: memorynotfound.com
ASCII to Hex: 6d656d6f72796e6f74666f756e642e636f6d
Hex to ASCII: memorynotfound.com

Resources

You may also like...