Convert Integer to Char in C++

Convert an integer code like 65 into its character 'A' in C++ with a C-style cast or static_cast. Also covers turning digit 7 into '7' by adding '0'.

BeginnerString Conversion ProgramsExample 2 of 11
convert-integer-to-char.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 65;
6
7 // Method 1: Direct casting
8 char ch1 = (char)num;
9
10 // Method 2: Using static_cast
11 char ch2 = static_cast<char>(num);
12
13 cout << "Integer: " << num << endl;
14 cout << "Character (method 1): " << ch1 << endl;
15 cout << "Character (method 2): " << ch2 << endl;
16
17 return 0;
18}

Sample Output

Character (method 1): A

Code 65 as a character.

In this one

  • That casting an int to char picks the character with that code
  • The difference between (char)num and static_cast<char>(num)
  • How to turn a digit value into a digit character
  • What happens with values outside 0–127

How it works

  1. 1

    num holds 65.

  2. 2

    (char)num gives 'A'.

  3. 3

    static_cast<char>(num) gives the same.

  4. 4

    Print the integer and both characters.

Edge cases

Digit value

Cast 7 to char.

You get the control character with code 7 (a bell), not '7'. Use char(7 + '0') to get the digit character.

Value above 127

Cast 200 to char.

char is often signed and 8 bits, so 200 does not fit as a positive value and prints as an odd symbol or nothing. Plain ASCII stops at 127.

What's going on

The program converts an integer (like 65) into its corresponding character (like 'A') based on the ASCII value. In the ASCII system, each character has a unique number:

65 represents 'A'
66 represents 'B'
97 represents 'a'
48 represents '0'

This conversion is essential when you need to display characters based on numeric values or work with character encoding.

Header File Used

#include <iostream>

This header provides:

cout for displaying output
cin for taking input (not used in this example, but available)

Declaring Variables

The program declares:

int num = 65;

int is the data type for storing whole numbers.
num stores the integer value 65, which corresponds to the character 'A' in ASCII.

Method 1: Direct Casting (C-style)

char ch1 = (char)num;

This method uses C-style casting:

(char) is the cast operator that converts the integer to a character.
The integer value is interpreted as an ASCII code.
So 65 becomes the character 'A'.

This is the simplest and most direct method, but it's considered less safe in modern C++ because it doesn't perform type checking.

Example:

num = 65 → ch1 = 'A'
num = 66 → ch1 = 'B'
num = 97 → ch1 = 'a'

Method 2: Using static_cast (C++ style)

char ch2 = static_cast<char>(num);

This method uses C++ style casting:

static_cast is a safer, more explicit way to convert types.
It performs compile-time type checking.
It's the recommended method in modern C++.

static_cast<char>(65) converts the integer 65 to the character 'A'.

This method is preferred over C-style casting because:

It's more readable
The compiler can catch type errors
It clearly shows the intent to convert types

Other Methods (Mentioned but not shown in code)

The program description mentions three additional methods:

Method 3: Using ASCII values directly

char ch3 = 65; // Direct assignment

This works because characters are stored as integers internally.

Method 4: Using char arithmetic

char ch4 = 'A' + (num - 65);

This calculates the character by adding an offset to a base character.

Method 5: Using sprintf

char buffer[2];

sprintf(buffer, "%c", num);

char ch5 = buffer[0];

This uses the C function sprintf to format the integer as a character.

Understanding ASCII Values

ASCII (American Standard Code for Information Interchange) is a character encoding standard:

Uppercase letters: A=65, B=66, ..., Z=90
Lowercase letters: a=97, b=98, ..., z=122
Digits: 0=48, 1=49, ..., 9=57
Special characters: Space=32, @=64, etc.

When you convert an integer to a character, C++ interprets that integer as an ASCII code and returns the corresponding character.

Displaying Results

The program prints:

cout << "Integer: " << num << endl;

cout << "Character (method 1): " << ch1 << endl;

cout << "Character (method 2): " << ch2 << endl;

Output:

Integer: 65

Character (method 1): A

Character (method 2): A

Both methods produce the same result because they both convert 65 to 'A'.

Important Considerations

Valid ASCII Range: Standard ASCII values range from 0 to 127. Extended ASCII goes up to 255. Values outside this range may produce unexpected results.

Type Safety: static_cast is safer than C-style casting because:

It performs compile-time checks
It's more explicit about the conversion
It helps prevent accidental type errors

Character vs Integer: Remember that:

Characters are stored as integers internally
Displaying a char shows the character
Displaying an int shows the number

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is static_cast<char>(97)?

Pick an answer to check it.

Question 2

How do you get the character '5' from int d = 5?

Pick an answer to check it.

Questions people ask

Why prefer static_cast?

It is easier to search for and the compiler rejects some unsafe conversions that a C-style cast would allow.

How do I print the number of a char?

Cast the other way: (int)ch or static_cast<int>(ch).

Is 'A' always 65?

On ASCII-based systems, which is effectively everything you will use.