Open In App

Convert char to int (Characters to Integers)

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Converting Characters to Integers is essential in programming, whether it is users’ input or extracting values from strings. Every programming language has different techniques to handle the conversion of these lines. In this article, we will explore how to convert characters to integers in several popular programming languages: C, C++, Java, Python, C#, and JavaScript.

Char to Int in C:

Through the basic arithmetic of the ASCII value subtracting ‘0’ from the character to get its value as an integer.

C
#include <stdio.h>

int main() {
    char ch = '5';
    int intValue = ch - '0';
    printf("The integer value is: %d\n", intValue);
    return 0;
}

Output
The integer value is: 5

Char to Int in C++:

Also like C, do character-to-integer conversion by subtracting the ASCII value of ‘0’ from the character.

C++
#include <iostream>
using namespace std;
int main() {
    char ch = '5';
    int intValue = ch - '0';
    cout << "The integer value is: "<< intValue;
    return 0;
}

Output
The integer value is: 5

Char to Int in Java:

Convert character to its integer representation by subtracting the ASCII value of ‘0’.

Java
public class Main {
    public static void main(String[] args) {
        char ch = '5';
        int intValue = ch - '0';
        System.out.println("The integer value is: " + intValue);
    }
}

Output
The integer value is: 5

Char to Int in Python:

Implement ord() function to get Unicode values and take off the Unicode value of ‘0’ from the character ‘5’ to obtain the integer representation.

Python3
ch = '5'
intValue = ord(ch) - ord('0')
print("The integer value is: ", intValue)

Output
The integer value is:  5

Char to Int in C#:

Exploiting the Unicode values, subtract the Unicode value of ‘0’ from the character to get its integer form.

C#
using System;

class Program {
    static void Main(string[] args) {
        char ch = '5';
        int intValue = ch - '0';
        Console.WriteLine($"The integer value is: {intValue}");
    }
}

Output
The integer value is: 5

Char to Int in Javascript:

Compute the character to integer value by subtracting the code of ‘0’ from the character code.

JavaScript
let ch = '5';
let intValue = ch.charCodeAt(0) - '0'.charCodeAt(0);
console.log(`The integer value is: ${intValue}`);

Output
The integer value is: 5

Applications of Converting char to int:

Converting characters to integers is a common operation in programming, especially in languages like C, C++, Java, and Python. Here are some applications of converting characters to integers:

  1. ASCII Value Representation: In many programming languages, characters are internally represented as ASCII (American Standard Code for Information Interchange) values. Converting a character to an integer allows you to work with its ASCII value directly.
  2. Comparing Characters: When comparing characters, you can convert them to integers and then compare their ASCII values. This is useful for sorting characters or checking their order in the ASCII table.
  3. Mathematical Operations: You can perform mathematical operations on characters by converting them to integers. For example, adding or subtracting integer values from characters can be used for character manipulation.
  4. Converting Characters to Digits: When dealing with numerical data input as characters (e.g., ‘0’ to ‘9’), converting these characters to integers allows you to perform arithmetic operations or use them in calculations.
  5. Converting Strings to Numbers: In languages like Python, converting characters to integers is essential for converting strings that represent numbers into actual numeric values. This is commonly used in input processing and data validation.

Conclusion:

Converting a character to an integer means obtaining its corresponding numeric value based on the character’s encoding, typically ASCII or Unicode. This conversion is often used in programming when dealing with characters in numerical contexts or when extracting numeric information from characters.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads