How to Convert Int to ASCII Char in C++

  1. Use Int to Char Assignment to Convert Int to ASCII Char
  2. Use sprintf() Function to Convert Int to ASCII Char

This article will explain several methods of how to convert int to ASCII char C++.

Use Int to Char Assignment to Convert Int to ASCII Char

ASCII character encoding is specified in a 7-bit format. Thus, there are 128 unique characters, each mapping to the corresponding numeric value from 0 to 127.

Since the C programming language implemented char types as numbers underneath the hood, we can assign an int variable to a char type variable and treat that as the corresponding ASCII character. For example, we directly push values from the int vector to the char vector and then output them using the std::copy method, displaying 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 <array>
#include <iterator>

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

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

    std::copy(numbers.begin(), numbers.end(),
                std::ostream_iterator<int>(cout, "; "));

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

    return EXIT_SUCCESS;
}

Output:

97; 98; 99; 100; 101; 102; 103;
a; b; c; d; e; f; g;

Alternatively, we can directly output int values as ASCII characters using printf function. printf takes format specifier for corresponding type argument as shown in the following table.

Specifier Description
% Prints a literal % character (this type doesn’t accept any flags, width, precision, length fields).
d, i int as a signed integer. %d and %i are synonymous for output, but are different when used with scanf for input (where using %i will interpret a number as hexadecimal if it is preceded by 0x, and octal if it is preceded by 0.)
u Print decimal unsigned int.
f, F double in normal (fixed-point) notation. f and F only differ in how the strings for an infinite number or NaN are printed (inf, infinity, and nan for f; INF, INFINITY and NAN for F).
e, E double value in standard form (d.ddde±dd). An E conversion uses the letter E (rather than e) to introduce the exponent.
g, G double in either normal or exponential notation, whichever is more appropriate for its magnitude. g uses lower-case letters, G uses upper-case letters.
x, X unsigned int as a hexadecimal number. x uses lower-case letters, and X uses upper-case.
o unsigned int in octal.
s null-terminated string.
c char (character).
p void* (pointer to void) in an implementation-defined format.
a, A double in hexadecimal notation, starting with 0x or 0X. a uses lower-case letters, A uses upper-case letters.
n Print nothing, but writes the number of characters successfully written so far into an integer pointer parameter.
#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;

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

    for (const auto &number : numbers) {
        printf("%c; ", number);
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

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

Use sprintf() Function to Convert Int to ASCII Char

The sprintf function is another method to convert int values to characters. We must declare a char variable to hold the converted value. This variable is passed as the first argument to sprintf and the third parameter specifies the number to be processed. Finally, you should provide one of the format specifiers as with printf, it indicates the type to which the value will be converted.

#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> numbers {97, 98, 99, 100, 101, 102, 103};

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

    return EXIT_SUCCESS;
}
a; b; c; d; e; f; g;

Related Article - C++ Int

  • How to Convert String to Int in C++