Convert ASCII to Char in C++

Convert ASCII to char in C++

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 this article, we will learn how to convert ASCII value to char in C++.

How to Convert ASCII values to Char in C++?

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.

We can convert the ASCII value to a character in C++ using multiple ways. We will discuss each of these ways one by one in this article.

  • We can use the char() function cast in C++ to convert ASCII to a character.
  • We can also use a C-style cast to convert an integer to its corresponding character.
  • We can use the assignment operator to convert ASCII code to a character in C++.
  • We can use the sprintf() function to directly print the character corresponding to the ASCII value in C++.

Further, we will understand how each method works and we will learn to use each of the above methods in our code. 

Convert ASCII to Char in C++ Using Char() Function

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.

You can see that the output is very much what we expect for the input we have given to the code. You should take note that when we provide an integer that does not have a character corresponding to it a garbage value is printed. We can see this in our last output.

Convert ASCII to Char in C++ Using C-Style Cast

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

The output of the code is given below. You 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. 

Convert ASCII to Char in C++ 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. You can 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++.

Convert ASCII to Character in C++ 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.

You should take note of the fact 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.

Conclusion

C++ is a popular and powerful programming language that is used worldwide for multiple applications. We can find it being used in embedded systems, the gaming industry, networking applications, etc. And the language does justice with its popularity by providing us tools and techniques to handle all the needs for programming.

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.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related Posts

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.