Get ASCII Value of Char in C++

  1. Use std::copy and std::ostream_iterator to Get ASCII Value of char
  2. Use printf Format Specifiers to Get ASCII Value of char
  3. Use int() to Get ASCII Value of char

This article will explain several methods of how to get the ASCII value of char in C++.

Use std::copy and std::ostream_iterator to Get ASCII Value of char

ASCII character encoding is almost ubiquitous in computers even though newer standard schemes emerged since then, like UTF-8 and others. Originally, ASCII encoded English letters, decimal digits, punctuation symbols, and some additional control codes. All these symbols are represented using some integer value from [0 - 127] range. Since the char types are implemented as integers, we can treat its values and output them to the cout stream using the ostream_iterator<int> and std::copy algorithm. Notice that consequent alphabet letters have neighboring number values.

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

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

int main() {
    vector<char> chars {'g', 'h', 'T', 'U', 'q', '%', '+', '!', '1', '2', '3'};

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

    return EXIT_SUCCESS;
}

Output:

chars: 103; 104; 84; 85; 113; 37; 43; 33; 49; 50; 51;

Use printf Format Specifiers to Get ASCII Value of char

The printf function is another alternative to output the char values with corresponding ASCII codes. Note that printf takes both arguments as char types and only differentiates format specifiers %c/%d. Even though the latter specifiers are usually documented as char and int formatters, respectively, they are compatible with each other’s types.

#include <iostream>
#include <vector>

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

int main() {
    vector<char> chars {'g', 'h', 'T', 'U', 'q', '%', '+', '!', '1', '2', '3'};

    cout << endl;
    for (const auto &number : chars) {
        printf("The ASCII value of '%c' is: %d\n", number, number);
    }

    return EXIT_SUCCESS;
}

Output:

The ASCII value of 'g' is: 103
The ASCII value of 'h' is: 104
The ASCII value of 'T' is: 84
The ASCII value of 'U' is: 85
The ASCII value of 'q' is: 113
The ASCII value of '%' is: 37
The ASCII value of '+' is: 43
The ASCII value of '!' is: 33
The ASCII value of '1' is: 49
The ASCII value of '2' is: 50
The ASCII value of '3' is: 51

Use int() to Get ASCII Value of char

Finally, one can convert char values using int(c) notation and output the values directly to the cout stream as any other variable. Note that this example is more C++ style than the printf version with its format specifiers. In contrast, the ASCII character values can be printed from the int values using the char(i) notation.

#include <iostream>
#include <vector>

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

int main() {
    vector<char> chars {'g', 'h', 'T', 'U', 'q', '%', '+', '!', '1', '2', '3'};

    cout << endl;
    for (auto &number : chars) {
        cout << "The ASCII value of '" << number << "' is: " << int(number) << endl;
    }

    return EXIT_SUCCESS;
}

Output:

The ASCII value of 'g' is: 103
The ASCII value of 'h' is: 104
The ASCII value of 'T' is: 84
The ASCII value of 'U' is: 85
The ASCII value of 'q' is: 113
The ASCII value of '%' is: 37
The ASCII value of '+' is: 43
The ASCII value of '!' is: 33
The ASCII value of '1' is: 49
The ASCII value of '2' is: 50
The ASCII value of '3' is: 51
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Char

  • Convert Char Array to Int in C++
  • Convert ASCII to Char in C++