Convert Hex to ASCII and ASCII to Hex

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 !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Convert Hex to ASCII and ASCII to Hex”

  1. I have tried the above code in my example but the problem is when converting back to hex from ASCII few of the characters were skipped.

    String bitmapString = “B0308101400080000000000004000020”;
    System.out.println(“bitmapString := “+bitmapString);

    String asciiValue = hexToASCII(bitmapString);
    System.out.println(“ASCII value := “+asciiValue);

    System.out.println(“Orginal bitMap value := “+asciiToHex(asciiValue));

    o/p : B0308101400080000000000004000020 converted as b030811400800000040020

    Appreciate your help here !!

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.