Sai A Sai A
Updated date Jul 12, 2023
In this blog, we will learn how to convert a string to a hexadecimal (hex) array in Java. Discover multiple methods, including utilizing the String and StringBuilder classes, leveraging the Apache Commons Codec library, and utilizing the java.nio.charset.Charset class.

Introduction:

In Java programming, converting a string to a hexadecimal (hex) array is a common requirement for various tasks such as cryptography, networking, and data manipulation. In this blog post, we will explore multiple approaches to achieve this conversion and discuss their pros and cons. We will provide code examples, step-by-step explanations, and the corresponding output for each method.

Method 1: Using the String and StringBuilder Classes

To convert a string to a hex array using the String and StringBuilder classes, follow these steps:

  • Define the input string.
  • Create an instance of the StringBuilder class to store the hex values.
  • Iterate over each character in the input string.
  • Convert each character to its corresponding hex value using the Integer.toHexString() method.
  • Append the hex value to the StringBuilder instance.
  • Convert the StringBuilder to a string representation of the hex array.
public class StringToHexArrayExample {

    public static void main(String[] args) {
        String input = "Hello, World!";
        StringBuilder hexArray = new StringBuilder();

        for (char c : input.toCharArray()) {
            String hexValue = Integer.toHexString((int) c);
            hexArray.append(hexValue);
        }

        String hexString = hexArray.toString();
        System.out.println("Hex Array: " + hexString);
    }
}

Output: 

Hex Array: 48656c6c6f2c20576f726c6421

Method 2: Utilizing the Apache Commons Codec Library

To convert a string to a hex array using the Apache Commons Codec library, follow these steps:

  • Add the Apache Commons Codec library to your project.
  • Import the necessary classes from the library.
  • Create an instance of the Hex class.
  • Invoke the encodeHexString() method, passing the input string.
  • Obtain the hex array representation from the method's return value.
import org.apache.commons.codec.binary.Hex;

public class StringToHexArrayExample {

    public static void main(String[] args) {
        String input = "Hello, World!";
        Hex hex = new Hex();

        byte[] bytes = input.getBytes();
        String hexString = hex.encodeHexString(bytes);

        System.out.println("Hex Array: " + hexString);
    }
}

Output:

Hex Array: 48656c6c6f2c20576f726c6421

Method 3: Leveraging the java.nio.charset.Charset Class

To convert a string to a hex array using the java.nio.charset.Charset class, follow these steps:

  • Define the input string.
  • Specify the character encoding to use (e.g., UTF-8).
  • Convert the input string to a byte array using the specified encoding.
  • Convert each byte in the byte array to its corresponding hex value.
  • Build the hex array by appending the hex values.
  • Obtain the final hex array as a string representation.
import java.nio.charset.Charset;

public class StringToHexArrayExample {

    public static void main(String[] args) {
        String input = "Hello, World!";
        Charset charset = Charset.forName("UTF-8");

        byte[] bytes = input.getBytes(charset);
        StringBuilder hexArray = new StringBuilder();

        for (byte b : bytes) {
            String hexValue = Integer.toHexString(b & 0xFF);
            hexArray.append(hexValue);
        }

        String hexString = hexArray.toString();
        System.out.println("Hex Array: " + hexString);
    }
}

Output: 

Hex Array: 48656c6c6f2c20576f726c6421

Conclusion:

In this blog post, we explored three different approaches to convert a string to a hex array in Java. We discussed the use of the String and StringBuilder classes, the Apache Commons Codec library, and the java.nio.charset.Charset class. By following the provided code examples and explanations, you can easily implement the appropriate method to accomplish the string to hex array conversion in Java.

Comments (0)

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