Sai A Sai A
Updated date Jul 28, 2023
In this blog, we will learn how to convert a regular string into a hexadecimal string array in Java. Explore multiple methods, including String.format() and StringBuilder, as well as leveraging the Apache Commons Codec library.

Introduction:

In Java, converting a string to a hexadecimal string array is a common task, often used in various applications like encryption, encoding, and networking. In this blog, we will delve into different methods to achieve this conversion and explore their advantages and limitations. 

Method 1: Using String Format and StringBuilder

Let's start with a simple yet efficient method using the String.format() and StringBuilder classes. This approach is straightforward and yields a readable and concise code. Here's the Java program implementing this method:

import java.util.Arrays;

public class Method1 {

    public static String[] convertStringToHexStringArray(String input) {
        StringBuilder hexString = new StringBuilder();
        for (char character : input.toCharArray()) {
            hexString.append(String.format("%02X ", (int) character));
        }
        return hexString.toString().trim().split(" ");
    }

    public static void main(String[] args) {
        String input = "Hello, World!";
        String[] hexArray = convertStringToHexStringArray(input);
        System.out.println("Method 1 Output: " + Arrays.toString(hexArray));
    }
}

Output:

Method 1 Output: [48, 65, 6C, 6C, 6F, 2C, 20, 57, 6F, 72, 6C, 64, 21]

In this method, we use a StringBuilder to build the hexadecimal string representation of each character in the input string. The loop iterates through the characters of the input string, and for each character, it converts the Unicode value to a hexadecimal representation using the %02X format specifier in String.format(). The %02X ensures that the output is zero-padded to two digits.

Method 2: Using Apache Commons Codec Library

For a more robust and feature-rich solution, we can leverage the Apache Commons Codec library. This library provides various encoding utilities, including hexadecimal encoding. Make sure to include the library in your project before using this method:

import org.apache.commons.codec.binary.Hex;
import java.nio.charset.StandardCharsets;

public class Method2 {

    public static String[] convertStringToHexStringArray(String input) {
        byte[] byteArray = input.getBytes(StandardCharsets.UTF_8);
        char[] hexArray = Hex.encodeHex(byteArray);
        return new String(hexArray).split("(?<=\\G.{2})");
    }

    public static void main(String[] args) {
        String input = "Hello, World!";
        String[] hexArray = convertStringToHexStringArray(input);
        System.out.println("Method 2 Output: " + Arrays.toString(hexArray));
    }
}

Output:

Method 2 Output: [48, 65, 6C, 6C, 6F, 2C, 20, 57, 6F, 72, 6C, 64, 21]

In this method, we use the getBytes() method to get the UTF-8 encoded byte array of the input string. Then, we utilize the Hex.encodeHex() method from the Apache Commons Codec library to convert the byte array to a hexadecimal character array. Finally, we split the character array into two-digit chunks using a regular expression, producing the desired hexadecimal string array.

Conclusion:

In this blog, we explored several methods to convert a string to a hexadecimal string array in Java. We started with a simple approach using String.format() and StringBuilder, which is ideal for basic conversions. Then, we stepped up the game by introducing the Apache Commons Codec library, providing a more feature-rich solution.

Comments (0)

There are no comments. Be the first to comment!!!