Convert Hexadecimal value String to ASCII value String
Given Hexadecimal value string as input, the task is to convert the given hexadecimal value string into their corresponding ASCII format string.
Examples:
Input: 6765656b73
Output: geeksInput: 6176656e67657273
Output: avengers
The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system. Being a Base-16 system, there are 16 possible digit symbols. Hexadecimal number uses 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all numbers. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15).
ASCII stands for American Standard Code for Information Interchange. ASCII is a standard that assigns letters, numbers, and other characters within the 256 slots available in the 8-bit code. E.g the lower case “h” character (Char) has a decimal value of 104, which is “01101000” in binary and “68” in hexadecimal.
For more ASCII values, refer ASCII TABLE.
Algorithm:
- Initialize final ascii string as empty.
- Extract first two characters from the hexadecimal string taken as input.
- Convert it into base 16 integer.
- Cast this integer to character which is ASCII equivalent of 2 char hex.
- Add this character to final string.
- Extract next two characters from hexadecimal string and go to step 3.
- Follow these steps to extract all characters from hexadecimal string.
// Java program to convert hexadecimal // string to ASCII format string import java.util.Scanner; public class HexadecimalToASCII { public static String hexToASCII(String hex) { // initialize the ASCII code string as empty. String ascii = ""; for (int i = 0; i < hex.length(); i += 2) { // extract two characters from hex string String part = hex.substring(i, i + 2); // change it into base 16 and typecast as the character char ch = (char)Integer.parseInt(part, 16); // add this char to final ASCII string ascii = ascii + ch; } return ascii; } public static void main(String[] args) { // print the ASCII string. System.out.println(hexToASCII("6765656b73")); } } |
Output:
geeks
Recommended Posts:
- Convert a string to hexadecimal ASCII values
- Convert the ASCII value sentence to its equivalent string
- Decode an Encoded Base 64 String to ASCII String
- Minimum number of given operations required to convert a string to another string
- Convert a List of String to a comma separated String in Java
- Print all possible ways to convert one string into another string | Edit-Distance
- Convert the string into palindrome string by changing only one character.
- Convert a Set of String to a comma separated String in Java
- Convert Set of String to Array of String in Java
- Average of ASCII values of characters of a given string
- Count pairs of characters in a string whose ASCII value difference is K
- Count characters in a string whose ASCII values are prime
- Check if a string contains only alphabets in Java using ASCII values
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Program to find the product of ASCII values of characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



