How to convert Hex to ASCII in Java
Written on January 20, 2010 at 2:22 pm by
mkyong
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
This article was posted in Java category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
Don’t re-invent the wheel, just use String.format.
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~