
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Convert a String to Hexadecimal and vice versa format in java?
In this article, we will learn how to convert a string to hexadecimal and hexadecimal to string in Java. Hexadecimal is a base-16 number system that uses 16 symbols to represent a number. Those symbols can be 0-9 and A-F letters.
String to Hexadecimal in Java
To convert a string to hexadecimal in Java, we can use the toHexString() method of the Integer class accepts an integer as a parameter and returns a hexadecimal string. Following are the steps to convert a string to hexadecimal:
- Get the desired String.
- Create an empty StringBuffer object.
- Convert it into a character array using the toCharArray() method of the String class.
- Traverse through the contents of the array created above, using a loop.
- Within the loop, convert each character of the array into an integer and pass it as a parameter to the toHexString() method of the Integer class.
- Append the resultant values to the StringBuffer object using the append() method of the StringBuffer class.
- Finally, convert the StringBuffer object to a string using the toString() method of the StringBuffer class
Example of String to Hexadecimal
Following is the Java program to convert a string to hexadecimal:
public class StringToHex{ public static void main(String args[]){ String str = "TutorialsPoint"; StringBuffer sb = new StringBuffer(); char ch[] = str.toCharArray(); for(int i = 0; i < ch.length; i++){ String hex = Integer.toHexString(ch[i]); sb.append(hex); } String ans = sb.toString(); System.out.println("The hexadecimal value of the string is: " + ans); } }
Output
When the above code is executed, it produces the following output:
The hexadecimal value of the string is: 5475746f7269616c73506f696e74
Hexadecimal to String in Java
To convert a hexadecimal string to a string in Java, we can use the parseInt() method of the Integer class. This method accepts a string and a radix as parameters and returns an integer. The following are the steps to convert a hexadecimal string to a string:
- Get the hexadecimal value (String).
- Convert it into a character array using the toCharArray() method.
- Read each two characters from the array and convert them into a String.
- Parse above obtained string into base 16 integer, cast it into a character.
- Concat all the characters to a string.
Example of Hexadecimal to String
Following is the Java program to convert a hexadecimal string to a string:
public class HexToString{ public static void main(String args[]){ String str = "5475746f7269616c73506f696e74"; StringBuffer sb = new StringBuffer(); char ch[] = str.toCharArray(); for(int i = 0; i < ch.length; i+=2){ String s = "" + ch[i] + ch[i+1]; char c = (char)Integer.parseInt(s, 16); sb.append(c); } System.out.println("The string value of the hexadecimal is: " + sb.toString()); } }
Output
When the above code is executed, it produces the following output:
The string value of the hexadecimal is: TutorialsPoint