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.
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.
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.
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.
Java
Java's method is similar to JavaScript's, using charAt() combined with explicit type casting.
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:
| Character | ASCII Value |
| A | 65 |
| B | 66 |
| a | 97 |
| b | 98 |
| 0 | 48 |
| 1 | 49 |
| @ | 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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.