Convert Hexadecimal value String to ASCII value String in C++


In this tutorial, we will be discussing a program to convert hexadecimal value string to ASCII value string.

For this we will be provided with a string with some hexadecimal values. Our task is to get that hexadecimal value and convert it into equivalent ASCII values.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
string convert_ASCII(string hex){
   string ascii = "";
   for (size_t i = 0; i < hex.length(); i += 2){
      //taking two characters from hex string
      string part = hex.substr(i, 2);
      //changing it into base 16
      char ch = stoul(part, nullptr, 16);
      //putting it into the ASCII string
      ascii += ch;
   }
   return ascii;
}
int main(){
   cout << convert_ASCII("6176656e67657273") << endl;
   return 0;
}

Output

avengers

Updated on: 22-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements