How to Convert ASCII to Char in C++

Jinku Hu Feb 02, 2024
  1. Use Assignment Operator to Convert ASCII Value to Char in C++
  2. Use the sprintf() Function to Convert ASCII Value to Char in C++
  3. Use char() to Convert ASCII Value to Char
How to Convert ASCII to Char in C++

This article will demonstrate multiple methods of how to convert ASCII values to chars in C++.

Use Assignment Operator to Convert ASCII Value to Char in C++

ASCII encoding supports 128 unique characters, and each is mapped to the corresponding character value. Since the C programming language implemented char types as numbers underneath the hood, we can assign corresponding int values to character variables. As an example, we can push values from the int vector to the char vector and then print them out to console using the std::copy algorithm, which will display ASCII characters as expected.

Note that assigning to char type only works when the int value corresponds to an ASCII code e.i. is in the range 0-127.

#include <charconv>
#include <iostream>
#include <iterator>
#include <vector>

using std::copy;
using std::cout;
using std::endl;
using std::vector;

int main() {
  vector<int> ascii_vals{97, 98, 99, 100, 101, 102, 103};
  vector<char> chars{};

  chars.reserve(ascii_vals.size());
  for (auto &n : ascii_vals) {
    chars.push_back(n);
  }
  copy(chars.begin(), chars.end(), std::ostream_iterator<char>(cout, "; "));

  return EXIT_SUCCESS;
}

Output:

a; b; c; d; e; f; g;

Use the sprintf() Function to Convert ASCII Value to Char in C++

The sprintf function is another method for converting ASCII values to characters. In this solution, we declare a char array to store the converted values each iteration until the printf outputs to the console. sprintf takes character array as the first argument. Next, you should provide a %c format specifier, which denotes a character value, and this parameter indicates the type to which the input will be converted. Finally, as the third parameter, you should supply source variable i.e. ASCII values.

#include <array>
#include <charconv>
#include <iostream>
#include <iterator>
#include <vector>

using std::array;
using std::copy;
using std::cout;
using std::endl;
using std::to_chars;
using std::vector;

int main() {
  vector<int> ascii_vals{97, 98, 99, 100, 101, 102, 103};

  array<char, 5> char_arr{};
  for (auto &n : ascii_vals) {
    sprintf(char_arr.data(), "%c", n);
    printf("%s; ", char_arr.data());
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

a; b; c; d; e; f; g;

Use char() to Convert ASCII Value to Char

Alternatively, char() cast can be used to convert individual ASCII values to char type. The following example demonstrates how one could output characters to the console directly from the int vector containing ASCII values.

#include <charconv>
#include <iostream>
#include <iterator>
#include <vector>

using std::copy;
using std::cout;
using std::endl;
using std::vector;

int main() {
  vector<int> ascii_vals{97, 98, 99, 100, 101, 102, 103};

  for (auto &n : ascii_vals) {
    cout << char(n) << endl;
  }

  return EXIT_SUCCESS;
}

Output:

a
b
c
d
e
f
g
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Char