ASCII Table and Character Representation in C++
- Snippet from Wikipedia: ASCII
ASCII (İngilizce: American Standard Code for Information Interchange, Türkçe: Bilgi Değişimi İçin Amerikan Standart Kodlama Sistemi) Latin alfabesi üzerine kurulu 7 bitlik bir karakter kümesidir. İlk kez 1963 yılında ANSI tarafından standart olarak sunulmuştur.
ASCII'de 33 tane basılmayan kontrol karakteri ve 95 tane basılan karakter bulunur. Kontrol karakterleri metnin akışını kontrol eden, ekranda çıkmayan karakterlerdir. Basılan karakterler ise ekranda görünen, okuduğumuz metni oluşturan karakterlerdir. ASCII'nin basılan karakterleri aşağıda belirtilmiştir.
In C++ programming, characters are stored in memory not as text, but as numerical values. The universal standard that defines how these numerical values correspond to characters is called ASCII.
The char type, the basic character data type in C++, is essentially an 8-bit integer type. It typically uses the ASCII table internally.
When we perform an operation on a character, we are actually operating on its ASCII value. This opens the door to powerful and fast manipulation techniques known as character arithmetic.
| Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol |
|---|---|---|---|---|---|---|---|
| 0 | NUL (null) | 32 | (space) | 64 | @ | 96 | ` |
| 1 | SOH (start of header) | 33 | ! | 65 | A | 97 | a |
| 2 | STX (start of text) | 34 | ” | 66 | B | 98 | b |
| 3 | ETX (end of text) | 35 | # | 67 | C | 99 | c |
| 4 | EOT (end of transmission) | 36 | $ | 68 | D | 100 | d |
| 5 | ENQ (enquiry) | 37 | % | 69 | E | 101 | e |
| 6 | ACK (acknowledge) | 38 | & | 70 | F | 102 | f |
| 7 | BEL (bell) | 39 | ’ | 71 | G | 103 | g |
| 8 | BS (backspace) | 40 | ( | 72 | H | 104 | h |
| 9 | HT (horizontal tab) | 41 | ) | 73 | I | 105 | i |
| 10 | LF (line feed/new line) | 42 | * | 74 | J | 106 | j |
| 11 | VT (vertical tab) | 43 | + | 75 | K | 107 | k |
| 12 | FF (form feed / new page) | 44 | , | 76 | L | 108 | l |
| 13 | CR (carriage return) | 45 | - | 77 | M | 109 | m |
| 14 | SO (shift out) | 46 | . | 78 | N | 110 | n |
| 15 | SI (shift in) | 47 | / | 79 | O | 111 | o |
| 16 | DLE (data link escape) | 48 | 0 | 80 | P | 112 | p |
| 17 | DC1 (data control 1) | 49 | 1 | 81 | Q | 113 | q |
| 18 | DC2 (data control 2) | 50 | 2 | 82 | R | 114 | r |
| 19 | DC3 (data control 3) | 51 | 3 | 83 | S | 115 | s |
| 20 | DC4 (data control 4) | 52 | 4 | 84 | T | 116 | t |
| 21 | NAK (negative acknowledge) | 53 | 5 | 85 | U | 117 | u |
| 22 | SYN (synchronous idle) | 54 | 6 | 86 | V | 118 | v |
| 23 | ETB (end of transmission block) | 55 | 7 | 87 | W | 119 | w |
| 24 | CAN (cancel) | 56 | 8 | 88 | X | 120 | x |
| 25 | EM (end of medium) | 57 | 9 | 89 | Y | 121 | y |
| 26 | SUB (substitute) | 58 | : | 90 | Z | 122 | z |
| 27 | ESC (escape) | 59 | ; | 91 | [ | 123 | { |
| 28 | FS (file separator) | 60 | < | 92 | \ | 124 | | |
| 29 | GS (group separator) | 61 | = | 93 | ] | 125 | } |
| 30 | RS (record separator) | 62 | > | 94 | ^ | 126 | ~ |
| 31 | US (unit separator) | 63 | ? | 95 | _ | 127 | DEL (delete) |
Character Arithmetic
Finding the ASCII Value of a Character
#include <iostream> int main() { char letter = 'A'; // Safe and modern C++ casting int asciiValue = static_cast<int>(letter); std::cout << "Character: " << letter << "\n"; std::cout << "ASCII Value: " << asciiValue << "\n"; // Output: 65 return 0; }
Uppercase / Lowercase Conversion
There is a difference of exactly 32 units between uppercase and lowercase letters in the ASCII table. (E.g., 'a' = 97, 'A' = 65 → 97 - 65 = 32). We can perform manual uppercase/lowercase conversions using this rule.
#include <iostream> int main() { char lowerCase = 'g'; // Subtracting 32 from a lowercase letter gives us the uppercase letter. char upperCase = lowerCase - 32; std::cout << "Uppercase of " << lowerCase << " is: " << upperCase << "\n"; // Output: Uppercase of g is: G return 0; }
Note: In modern C++ projects, instead of doing this manually, it is best practice to use the std::toupper() and std::tolower() functions from the <cctype> library.
Checking if a Character is a Number
We can perform a mathematical validation by checking if the character's ASCII value is between 48 ('0') and 57 ('9').
#include <iostream> bool isDigit(char c) { // Check if it is within the '0' to '9' ASCII range return (c >= '0' && c <= '9'); } int main() { char testCharacter = '5'; if (isDigit(testCharacter)) { std::cout << testCharacter << " is a digit.\n"; } return 0; }
The information in this document is cited from UCH Wiki. https://wiki.ulascemh.com/doku.php?id=en:cs:lang:cpp:types:ascii