Convert Hex to ASCII and ASCII to Hex

Lokesh Gupta

Many times we do need to convert the string values ASCII from/to Hex format. In this small, post I am giving you two small code snippets which 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

Convert ASCII to Hex

It is done in following steps:

  1. Convert String to char array
  2. Cast it to Integer
  3. Use Integer.toHexString() to convert it to Hex

ASCII to Hex Example Code

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();
}

Convert Hex to ASCII

It is done in following steps:

  1. Cut the Hex value in 2 chars groups
  2. Convert it to base 16 Integer using Integer.parseInt(hex, 16) and cast to char
  3. Append all chars in StringBuilder

Hex to ASCII Example Code

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 above methods with sample input data.

Complete Example for Hex to ASCII and ASCII to Hex Conversion

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

package test.core;

public class HexAsciiConversionExamples
{
   public static void main(String[] args)
   {
      String demoString = "//howtodoinjava.com";
      //Original String
      System.out.println("Original String: "+ demoString);
      
      String hexEquivalent = asciiToHex(demoString);
      //Hex value of original String
      System.out.println("Hex String: "+ hexEquivalent);
      
      String asciiEquivalent = hexToASCII(hexEquivalent);
      //ASCII value obtained from Hex value
      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();
   }
}

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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode