ASCII
Character encoding
Programming
Coding tutorial
Computer Science

How to get the ASCII value of a character

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

The American Standard Code for Information Interchange (ASCII) is a character encoding standard used to represent text in computers and other digital devices. In ASCII, each character (letter, numeral, symbol) is represented by a unique numeric value. Understanding how to retrieve the ASCII value of characters can be essential for various computing tasks, such as programming, data processing, and debugging.

Understanding ASCII

ASCII originally was designed to include 128 character codes ranging from 0 to 127. These include 95 printable characters and 33 non-printable control characters (such as carriage return, line feed, etc.). The printable characters include:

  • Uppercase letters (A-Z)
  • Lowercase letters (a-z)
  • Digits (0-9)
  • Punctuation marks
  • Other special characters (such as $, @, and *)

Each character is assigned a unique decimal number. For example, the ASCII value for the uppercase letter "A" is 65, and for the lowercase "a" it is 97.

Retrieving ASCII Values in Different Programming Languages

The process to get the ASCII value of a character can vary slightly depending on the programming language you are using. Here are examples in some popular programming languages:

Python

Python includes a function called ord() which returns the Unicode code point for a single character. For standard ASCII, this is the same as the ASCII value.

python
char = 'A'
ascii_value = ord(char)
print(ascii_value)  # Output: 65

JavaScript

In JavaScript, the method to use is charCodeAt(). This method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

javascript
let char = 'A';
let ascii_value = char.charCodeAt(0);
console.log(ascii_value);  // Output: 65

C and C++

In C or C++, you can directly convert a character to its ASCII integer equivalent by type-casting the character to an int.

cpp
char chara = 'A';
int ascii_value = (int) chara;
printf("%d", ascii_value);  // Output: 65

Java

Java's method is similar to JavaScript's, using charAt() combined with explicit type casting.

java
char chara = 'A';
int ascii_value = (int) chara;
System.out.println(ascii_value);  // Output: 65

Common Uses of ASCII Values

ASCII values are used in various computing scenarios including:

  • Sorting algorithms
  • Character encoding
  • Data validation
  • Cryptography
  • Implementing communication protocols

ASCII Table Summary

Here is a summary of some commonly used ASCII characters and their corresponding decimal values:

CharacterASCII Value
A65
B66
a97
b98
048
149
@64
#35
------
%37

Conclusion

Determining the ASCII value of a character is a fundamental skill in programming and computer science, useful in many contexts from low-level memory management to high-level data processing. Utilizing basic functions provided by programming languages, developers can easily encode, decode, and manipulate textual data according to the needs of various applications. As technology evolves, while Unicode and other encodings have surpassed ASCII in usage, the principles learned through ASCII are foundational and provide a stepping stone to understand more complex encoding standards.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions