How to convert Hex to ASCII in Java

Here’s a Java example to show how to convert Hex to ASCII or vice verse in Java. The conversion process is depend on this formula “Hex<==>Decimal<==>ASCII“.

  • ASCII to Hex – Convert String to char array, cast it to integer(decimal) follow by Integer.toHexString to convert it to Hex value.
  • Hex to ASCII – Cut the Hex value in pairs format, convert it to radix 16 interger(decimal) Integer.parseInt(hex, 16), and cast it back to char.

Example

public class StringToHex{
 
  public String convertStringToHex(String str){
 
	  char[] chars = str.toCharArray();
 
	  StringBuffer hex = new StringBuffer();
	  for(int i = 0; i < chars.length; i++){
	    hex.append(Integer.toHexString((int)chars[i]));
	  }
 
	  return hex.toString();
  }
 
  public String convertHexToString(String hex){
 
	  StringBuilder sb = new StringBuilder();
	  StringBuilder temp = new StringBuilder();
 
	  //49204c6f7665204a617661 split into two characters 49, 20, 4c...
	  for( int i=0; i<hex.length()-1; i+=2 ){
 
	      //grab the hex in pairs
	      String output = hex.substring(i, (i + 2));
	      //convert hex to decimal
	      int decimal = Integer.parseInt(output, 16);
	      //convert the decimal to character
	      sb.append((char)decimal);
 
	      temp.append(decimal);
	  }
	  System.out.println("Decimal : " + temp.toString());
 
	  return sb.toString();
  }
 
  public static void main(String[] args) {
 
	  StringToHex strToHex = new StringToHex();
	  System.out.println("\n***** Convert ASCII to Hex *****");
	  String str = "I Love Java!";  
	  System.out.println("Original input : " + str);
 
	  String hex = strToHex.convertStringToHex(str);
 
	  System.out.println("Hex : " + hex);
 
	  System.out.println("\n***** Convert Hex to ASCII *****");
	  System.out.println("Hex : " + hex);
	  System.out.println("ASCII : " + strToHex.convertHexToString(hex));
  }
}

Output

***** Convert ASCII to Hex *****
Original input : I Love Java!
Hex : 49204c6f7665204a61766121
 
***** Convert Hex to ASCII *****
Hex : 49204c6f7665204a61766121
Decimal : 7332761111181013274971189733
ASCII : I Love Java!

Reference

1. http://en.wikipedia.org/wiki/Hexadecimal
2. http://mindprod.com/jgloss/hex.html

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Arun

    But this creates a problem for a hex string with value between 80 to 9F.

    I am doing byte dump of the ASCII and am seeing if it has the same hexa value. For values between 80 to 9F, doesnt happen so.

    Any ideas.?

  • http://abc khalid

    So nice

  • http://none Gaius

    Hi Mkyoung,
    thank you so much for the tutorials you keep providing. It is of great help to a lot of people and we are greatful for the time and effort you put into all these.Please i was wondering if you can assist with Encryption steps code or tutorial. I am writing a project that should spell out all the setps involved in DES encryption from input through the Initial Permutaion, the 16 Rounds involved, down to the final output. But to show all details in all the steps. Would you have any advice or guidance, tutorial or code to that regard? most of the tips online always go straight to encrypt and decrypt but there is really no much on the individual steps. Any help will be greatly appreciated. Thanks in advace

    Gaius

  • preechaw

    I think the method convertStringToHex may need a little modification.
    It doesn’t handle hex below 0x10.
    For example, if the argument is “I\tlove Java!”, the second byte will become just “9” instead of “09”.

    public String convertStringToHex(String str){

    char[] chars = str.toCharArray();

    StringBuffer hex = new StringBuffer();
    int each;
    for(int i = 0; i < chars.length; i++){
    each = (int)chars[i];
    if (each < 0x10) // in this case, ASCII code is 1 digit hex
    {
    hex.append("0");
    }
    hex.append(Integer.toHexString(each));
    }

    return hex.toString();
    }

  • Techbrainless

    Thanks mkyong , It is really amazing post.

    your website is one of my top 10 java references sites
    Keep it up….:)

  • Himanshu

    superb code, thanks.

  • http://www.elbuild.it Lu

    Works fine even with extended ASCII (èéòà..). It saves me 15 minutes to write it on my own :)

  • Stanislav

    Thank you for Hex to String function. It save my day!

  • Guy Mac

    Don’t re-invent the wheel, just use String.format.

    • http://www.mkyong.com mkyong

      Hi,

      i go through the API again, String.format is use to format the String only, may i know how do convert String to Hex and vise verse? Can you provide a short example? Thanks~