Convert Hexadecimal value String to ASCII value String
Last Updated :
08 Feb, 2024
Given the Hexadecimal value string as input, the task is to convert the given hexadecimal value string into its corresponding ASCII format string.
Examples:
Input: 6765656b73
Output: geeks
Input: 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. The 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.
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.

Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string hexToASCII(string hex)
{
string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2)
{
string part = hex.substr(i, 2);
char ch = stoul(part, nullptr, 16);
ascii += ch;
}
return ascii;
}
int main()
{
cout << hexToASCII("6765656b73") << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class HexadecimalToASCII {
public static String hexToASCII(String hex)
{
String ascii = "";
for (int i = 0; i < hex.length(); i += 2) {
String part = hex.substring(i, i + 2);
char ch = (char)Integer.parseInt(part, 16);
ascii = ascii + ch;
}
return ascii;
}
public static void main(String[] args)
{
System.out.println(hexToASCII("6765656b73"));
}
}
|
Python3
def hexToASCII(hexx):
ascii = ""
for i in range(0, len(hexx), 2):
part = hexx[i : i + 2]
ch = chr(int(part, 16))
ascii += ch
return ascii
if __name__ == "__main__":
print(hexToASCII("6765656b73"))
|
C#
using System;
class GFG
{
public static String hexToASCII(String hex)
{
String ascii = "";
for (int i = 0; i < hex.Length; i += 2)
{
String part = hex.Substring(i, 2);
char ch = (char)Convert.ToInt32(part, 16);;
ascii = ascii + ch;
}
return ascii;
}
public static void Main(String[] args)
{
Console.WriteLine(hexToASCII("6765656b73"));
}
}
|
Javascript
<script>
function hexToASCII(hex) {
var ascii = "";
for (var i = 0; i < hex.length; i += 2) {
var part = hex.substring(i, i + 2);
var ch = String.fromCharCode(parseInt(part, 16));
ascii = ascii + ch;
}
return ascii;
}
document.write(hexToASCII("6765656b73"));
</script>
|
Time complexity: O(N), where N is the length of the given string
Auxiliary space: O(N)
Approach 2: Using Bitwise Operations:
This approach is to use bitwise operations to directly convert the hexadecimal string to an ASCII string. In this approach, we would start by converting the hexadecimal string to a series of bytes. We can do this by iterating through the string and converting each pair of hexadecimal digits to a byte. Once we have the bytes, we can use bitwise operations to convert them to characters in the ASCII string.
In this implementation, we use a stringstream to build the ASCII string. We iterate through the hexadecimal string, converting each pair of hexadecimal digits to a byte using stoi. Then, we append the byte to the stringstream. Finally, we return the contents of the stringstream as the ASCII string.
Here is the code of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
string hexToASCII(std::string hex) {
stringstream ss;
for (size_t i = 0; i < hex.length(); i += 2) {
unsigned char byte =stoi(hex.substr(i, 2), nullptr, 16);
ss << byte;
}
return ss.str();
}
int main() {
string hexString = "6765656b73";
string asciiString = hexToASCII(hexString);
cout << asciiString << endl;
return 0;
}
|
Java
import java.util.*;
public class HexToASCII {
public static String hexToASCII(String hex) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hex.length(); i += 2) {
String str = hex.substring(i, i + 2);
char ch = (char) Integer.parseInt(str, 16);
sb.append(ch);
}
return sb.toString();
}
public static void main(String[] args) {
String hexString = "6765656b73";
String asciiString = hexToASCII(hexString);
System.out.println(asciiString);
}
}
|
Python3
def hex_to_ascii(hex_str):
ascii_str = ""
for i in range(0, len(hex_str), 2):
byte = int(hex_str[i:i+2], 16)
ascii_str += chr(byte)
return ascii_str
hex_string = "6765656b73"
ascii_string = hex_to_ascii(hex_string)
print(ascii_string)
|
C#
using System;
using System.Text;
public class Program
{
public static string HexToASCII(string hex)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hex.Length; i += 2)
{
byte b = Convert.ToByte(hex.Substring(i, 2), 16);
sb.Append((char)b);
}
return sb.ToString();
}
public static void Main()
{
string hexString = "6765656b73";
string asciiString = HexToASCII(hexString);
Console.WriteLine(asciiString);
}
}
|
Javascript
function hexToASCII(hex) {
let sb = "";
for (let i = 0; i < hex.length; i += 2) {
let str = hex.substring(i, i + 2);
let ch = String.fromCharCode(parseInt(str, 16));
sb += ch;
}
return sb;
}
let hexString = "6765656b73";
let asciiString = hexToASCII(hexString);
console.log(asciiString);
|
Output
geeks
Time complexity: O(n), where N is the length of the given string
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...