Development

How To Get The ASCII Value of Char in C++

Captain Salem 2 min read

How To Get The ASCII Value of Char in C++

ASCII is one of the most fundamental and renowned character encoding standards used to represent text in systems and other devices that use text. Well, there’s Unicode but ASCII is much older and very basic that almost system can read it. In ASCII encoding, each character is represented by a number between 0 and 127.

As a developer, you will often come across instances where you need to convert a given ASCII value back it’s character representation and more. For example when reading keyboard input, you can read the ASCII value and then convert it back to its character equivalent before displaying it to the user.

In this tutorial, we will guide you through some common methods you can use to convert an ASCII value back to its character representation in C++.

Method 1 - Using Implicit Typecasting

The first method we can use to convert an ASCII value to a character is using implicit type conversion. In C++, we assign a char variable in the context that requires an integer, C++ will automatically convert it to its ASCII value.

Consider the example demonstration shown below:

#include <iostream>
int main() {
    char c = 'A';
    int asciiValue = c;

    std::cout << "The ASCII value of " << c << " is " << asciiValue << std::endl;

    return 0;
}

We can then run the code above:

g++ ascii_convert.cpp

Run the resulting binary:

./a.out

The resulting output is as shown:

The ASCII value of A is 65

Method 2 - Using static_cast Function

We can also use the static_cast operator to explicitly convert a char into its corresponding ASCII value. An example demonstration is as shown:

#include <iostream>

int main() {
    char c = 'A';
    int asciiValue = static_cast<int>(c);

    std::cout << "The ASCII value of " << c << " is " << asciiValue << std::endl;

    return 0;
}

Method 3 - Using Bitwise Operators

We also have access Bitwise operators which we can use to manipulate the data at the bit level. Consider the example below where we are using bitwise AND (&) with 0xff (255 in decimal) to get the ASCII value.

#include <iostream>

int main() {
    char c = 'A';
    int asciiValue = c & 0xff;

    std::cout << "The ASCII value of " << c << " is " << asciiValue << std::endl;

    return 0;
}

NOTE: Bit manipulation can be useful in cases where negative char values can occur as it ensures that the ASCII value is always positive.

End.

In this tutorial, we discussed three main methods that we can use to convert an input ASCII value to its character equivalent using the C++ programming language.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.