How to Convert ASCII to Char in C++

  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

This article will demonstrate multiple methods about 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 <iostream>
#include <vector>
#include <iterator>
#include <charconv>

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

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 <iostream>
#include <vector>
#include <array>
#include <iterator>
#include <charconv>

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

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 <iostream>
#include <vector>
#include <iterator>
#include <charconv>

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

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

Related Article - C++ Char

  • How to Convert String to Char Array in C++
  • How to Convert Char Array to String in C++