Convert ASCII to Char in C++

Convert ASCII to char in C++

1. Introduction

The characters are represented using integers in most of the encoding standards. Therefore, we can convert the characters to the integers and integers to the characters. ASCII stands for American Standard Code for Information Interchange. It is a character encoding scheme that we use to represent characters in computer systems. In the ASCII system, different characters are given different integer codes. For example, A is represented by 65, 0 is represented by 48, and a is represented by 97. Originally there are 128 ASCII characters and we also have special characters included in the ASCII table such as %, $, etc.

In C++, we represent characters using ASCII codes. However, the internal ASCII codes are not visible when we store characters into char data type instead we see the character itself.

In this article, we will learn how to convert ASCII value to char in C++. Further, we will understand how each method works and we will learn to use each of the above methods in our code. 

2. Using the static_cast Operator

The static_cast operator is a type casting method that can be used for the conversion of ASCII values to characters. This method is type-safe, ensuring that the conversion is valid at compile-time.

In this example, the integer asciiValue holding the ASCII value 65 is converted to the character ‘A’ using static_cast<char>(). The char keyword specifies the target type for the conversion.

3. Using Function-Style Casting

Function-style casting is similar to using constructors in object-oriented programming. It’s a direct and readable way to perform the conversion.

In C++ we have a char() function cast that we can use to convert an ASCII value to its corresponding character. The char() function cast takes an integer as the input and converts it to a character corresponding to the integer.

The following code demonstrates how we can use the char() function cast to convert an integer to a character.

The output of the above code is given as follows.

We can see that the output aligns well with the input we have provided to the code. It’s important to note that when we provide an integer without a corresponding character, a garbage value is printed, as observed in our last output.

4. Using C-Style Cast

We have already seen above that the function style char() cast can convert an ASCII value to the corresponding integer. However, this is not the only method C++ provides us. C++ also supports old C-style casts, with which we might already be familiar. In this method, we take an integer and explicitly cast it to the char type using the char keyword. Then, we assign the character to a variable of type char. By doing this, we can convert our ASCII value to its corresponding character in C++, as demonstrated below.

The output of the code is given below. We can see the output is the same as the output in the previous approach.

As we can see that last output is a garbage character because it corresponds to a value for which character does not exist. 

5. Using the Assignment Operator

We have already seen two ways to convert the ASCII value to its corresponding character. Those two ways are very similar but we are going to see a very simple and equally efficient way to do the same task.

In C++ we can use assignment operators to convert an ASCII integer value to its corresponding character. All we have to do is to use the assignment operator to assign an integer value to a variable with the char data type. This will assign a character corresponding to the ASCII integer value to the variable. It makes us able to convert an ASCII value to char without using type casting in C++ as it is handled by the compiler internally. Let’s see the following code that uses this approach.

The output of the above code is given below.

The output of the code is the same as the outputs in the previous approaches. It is because we have given the same input. It shows that this method works well and converts the ASCII integer value to the corresponding character in C++.

6. Using sprintf() Function

In all the ways that we have discussed above, we have to assign the converted ASCII value to a char data type. However, if we want to directly print the character derived from the ASCII integer value, we can do it by using the sprintf() function. We can also assign the character to a variable using the sprintf() function. 

The C++ language provides us with the sprintf() function that lets us write the formatted strings and characters to a buffer. We can use this function to convert our integer ASCII value to its corresponding character. The sprintf() function takes three arguments.

  • This first input argument takes a pointer to a buffer where the resultant of the formatted string is stored. Here, we will use a character variable as the buffer. 
  • The second input argument takes a string denoting the format specifier. We will use “%c” as the format specifier.
  •  We will pass the integer ASCII value as the third argument.

After execution of the sprintf() function, the corresponding char value for the input ASCII value will be assigned to the character variable that has been used as the first input argument.

The following code shows how we can use the sprintf() function in C++ to convert ASCII value to char.

The output of the code given above is given below. You can see the output is the same and perfect as our given input.

It is important to note that the sprintf() function is used to format the strings and characters and we, therefore, make use of this smartly. It is not exclusively provided for converting the ASCII integer value to its corresponding character.

7. Conclusion

In this article, we have witnessed one such useful application of C++ where we can convert an ASCII integer value to its corresponding character in different ways. We have talked about four different ways which are easy and efficient to perform the operation. This operation seems to be simple but it is a widely used and useful operation and we need it frequently when working with characters.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *