Java ASCII to Hex Conversion (and vice-versa)

ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numerical values to characters. Each character is represented by a unique numerical value ranging from 0 to 127. Similarly, Hexadecimal (or hex) is a base-16 numbering system that uses 16 distinct symbols: 0-9 and A-F (or a-f) to represent values. Each digit in a hexadecimal number represents four bits, allowing a compact representation of binary data.

Many times we do need to convert the string values in ASCII from/to Hex format. In this Java tutorial, I am giving you two small code snippets that you can utilize to convert a string from Hex to ASCII or ASCII to Hex, as you want.

Overall conversion logic looks like this:

Hex   <-->  Decimal  <-->  ASCII

1. Converting ASCII to Hex

The conversion from ASCII to Hexadecimal is done in the following steps:

  • Convert String to char array
  • Iterate through the array, converting each character to an integer
  • Utilize Integer.toHexString() to transform the integer into hexadecimal format

Java Program to convert ASCII String to Hexadecimal

public static String asciiToHex(String asciiValue) {

  char[] chars = asciiValue.toCharArray();
  StringBuffer hex = new StringBuffer();

  for (int i = 0; i < chars.length; i++) {
    hex.append(Integer.toHexString((int) chars[i]));
  }
  return hex.toString();
}

2. Convert Hex to ASCII

The conversion from hexadecimal to ASCII is done in the following steps:

  • Split the hexadecimal value into groups of two characters
  • Convert each group to its corresponding integer value in base 16 using Integer.parseInt(hex, 16) and cast it to a character
  • Concatenate all characters together in a StringBuilder.

We split the hexadecimal value into groups of two characters because each character in the hexadecimal representation represents four bits of data. Therefore, by grouping them in pairs, we ensure that each group represents a byte (eight bits) of data.

Java Program to convert Hex String to ASCII

private static String hexToASCII(String hexValue) {

  StringBuilder output = new StringBuilder("");

  for (int i = 0; i < hexValue.length(); i += 2) {
    String str = hexValue.substring(i, i + 2);
    output.append((char) Integer.parseInt(str, 16));
  }
  return output.toString();
}

Now let’s test the above methods with sample input data.

3. Complete Example

In this example, I am converting a string “//howtodoinjava.com” to first in hex format, and then converting that hex string to again in ASCII value. This converted ASCII value should be equal to the original string i.e. “//howtodoinjava.com”.

public class HexAsciiConversionExamples {

   public static void main(String[] args) {

      String demoString = "//howtodoinjava.com";
      System.out.println("Original String: "+ demoString);
       
      String hexEquivalent = asciiToHex(demoString);
      System.out.println("Hex String: "+ hexEquivalent);
       
      String asciiEquivalent = hexToASCII(hexEquivalent);
      System.out.println("Ascii String: "+ asciiEquivalent);
   }
 
   private static String asciiToHex(String asciiValue) {
      char[] chars = asciiValue.toCharArray();
      StringBuffer hex = new StringBuffer();
      for (int i = 0; i < chars.length; i++)
      {
         hex.append(Integer.toHexString((int) chars[i]));
      }
      return hex.toString();
   }
 
   private static String hexToASCII(String hexValue) {
      StringBuilder output = new StringBuilder("");
      for (int i = 0; i < hexValue.length(); i += 2)
      {
         String str = hexValue.substring(i, i + 2);
         output.append((char) Integer.parseInt(str, 16));
      }
      return output.toString();
   }
}

The program output:

Original String: //howtodoinjava.com
Hex String: 687474703a2f2f686f77746f646f696e6a6176612e636f6d
Ascii String: //howtodoinjava.com

Happy Learning !!

Comments

Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.