Sai A Sai A
Updated date Jul 29, 2023
In this blog, we will learn how to convert Java strings into hexadecimal string arrays. This blog explores multiple methods for achieving the conversion, including using StringBuilder and String.format(), as well as leveraging the BigInteger class.

Introduction:

In Java programming, converting a regular string to a hexadecimal string array is a common task with various practical applications, such as encryption, networking, and data transmission. In this blog, we will explore multiple methods to achieve this conversion, providing step-by-step examples and explanations for each approach.

Method 1: Using StringBuilder and String.format()

The first method involves using a StringBuilder and the String.format() function to convert each character in the input string to its corresponding hexadecimal representation. Let's implement this approach:

import java.util.Arrays;

public class HexConversionMethod1 {
    public static String[] convertToHexArray(String input) {
        StringBuilder hexStringBuilder = new StringBuilder();

        for (char c : input.toCharArray()) {
            hexStringBuilder.append(String.format("%02X", (int) c));
        }

        String hexString = hexStringBuilder.toString();
        return hexString.split("(?<=\\G.{2})");
    }

    public static void main(String[] args) {
        String inputString = "Hello, World!";
        String[] hexArray = convertToHexArray(inputString);
        System.out.println("Input String: " + inputString);
        System.out.println("Hexadecimal String Array: " + Arrays.toString(hexArray));
    }
}

Output:

Input String: Hello, World!
Hexadecimal String Array: [48, 65, 6C, 6C, 6F, 2C, 20, 57, 6F, 72, 6C, 64, 21]
  • We start by creating a StringBuilder named hexStringBuilder to accumulate the hexadecimal characters of the input string.
  • Next, we iterate through each character of the input string using the toCharArray() method.
  • For each character, we use String.format("%02X", (int) c) to convert it to its hexadecimal representation with two characters (padded with zeros if needed). The (int) c part converts the character to its integer Unicode value.
  • The resulting hexadecimal string is stored in the hexString variable.
  • Finally, we split the hexString into an array of strings of two characters each using the split() method and the regular expression "(?<=\\G.{2})".
  • The output is the hexadecimal string array, which represents each character of the input string in its hexadecimal form.

Method 2: Using BigInteger

The second method involves using the BigInteger class in Java, which provides a convenient way to convert strings to their hexadecimal representation.

import java.math.BigInteger;
import java.util.Arrays;

public class HexConversionMethod2 {
    public static String[] convertToHexArray(String input) {
        byte[] bytes = input.getBytes();
        String[] hexArray = new String[bytes.length];

        for (int i = 0; i < bytes.length; i++) {
            hexArray[i] = String.format("%02X", bytes[i]);
        }

        return hexArray;
    }

    public static void main(String[] args) {
        String inputString = "Hello, World!";
        String[] hexArray = convertToHexArray(inputString);
        System.out.println("Input String: " + inputString);
        System.out.println("Hexadecimal String Array: " + Arrays.toString(hexArray));
    }
}

Output:

Input String: Hello, World!
Hexadecimal String Array: [48, 65, 6C, 6C, 6F, 2C, 20, 57, 6F, 72, 6C, 64, 21]
  • We start by converting the input string to a byte array using the getBytes() method. This method converts the string to an array of bytes using the default character encoding of the platform.
  • We create a new string array called hexArray to store the hexadecimal representations of each byte in the input string.
  • Next, we iterate through each byte in the bytes array.
  • For each byte, we use String.format("%02X", bytes[i]) to convert it to its hexadecimal representation with two characters (padded with zeros if needed).
  • The resulting hexadecimal string is stored in the hexArray.
  • The output is the hexadecimal string array, where each element represents the hexadecimal value of a byte in the input string.

Conclusion:

In this blog, we explored various methods for converting Java strings to hexadecimal string arrays. We started with the StringBuilder and String.format() approach, which allows us to directly convert characters to their hexadecimal representation. Next, we used the BigInteger class and byte arrays to achieve the conversion.

Comments (0)

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