ASCII Table in C++

In this article, we will discuss ASCII Table and you will learn how to find an ASCII value of a character in C++. We will also discuss various programs to print an ASCII value of a character.

What is ASCII Table?

The ASCII(American Standard Code for Information Interchange) table is a collection of 256 symbols in the character set that is divided into two parts: the first part is standard ASCII code and the second part is extended ASCII code. The regular ASCII code is 7 bits long and has a range from 0 to 127, while the extended ASCII code is 8 bits long and has a range from 128 to 255. This character set consists of uppercase(A to Z) and lowercase(a to z) alphabets, digits(0-9), and 33 special characters involving mathematical symbols, punctuation marks, and the space character.

Suppose we are given a string "PrepBytes", the system does not store the string as it is. Instead, the computer stores the equivalent ASCII value of each character, such as ‘8011410111266121116101115’. The ASCII value of P is 80, r is 114, e is 101, and so on.

In total, there are 256 ASCII characters which can be divided into three types:

  • ASCII control characters (0-31 and 127)
  • ASCII printable characters (32-126) (most commonly used)
  • Extended ASCII characters (128-255)

ASCII Control Characters (0–31 & 127):

Control characters are not printable characters. Control characters are mainly used to send commands to the PC or the printer and are based on telex technology. With these characters, we can apply line breaks or tabs. Nowadays, they are out of use.

ASCII Printable Characters (32–126):

Printable characters can be further divided into three sub-categories:

  1. Special Characters(32–47 && 58–64 && 91–96 && 123–126): Special characters include all characters that are neither alphabets nor numbers. These include punctuation and mathematical characters. ASCII also includes the space character which is a non-visible but printable character, and therefore, space is not a member of the control characters category, as one might think.
  2. Numbers (48–57): These numbers include the ten Arabic numerals from 0-9.
  3. Alphabets(65–90 && 97–122): Alphabets are divided into two groups, the first group contains uppercase letters and the second group contains lowercase.

Below are the ASCII values of Printable Characters (32, 127):

Program to Print ASCII value of 0 to 9

#include <iostream>
using namespace std;

int main() {
    // program to print ASCII value of 0 to 9
    for(int i=0;i<=9;i++){
        cout<<"ASCII value of "<<i<<" is "<<i+48<<endl;
    }
    return 0;
}

Output:

ASCII value of 0 is 48
ASCII value of 1 is 49
ASCII value of 2 is 50
ASCII value of 3 is 51
ASCII value of 4 is 52
ASCII value of 5 is 53
ASCII value of 6 is 54
ASCII value of 7 is 55
ASCII value of 8 is 56
ASCII value of 9 is 57

Working:

  • In this program, we add 48 to get the ASCII value of each number from 0 to 9. We are running For loop 10 times and print the equivalent ASCII value of each number.
  • The Time Complexity of the above program is O(1), as we are running the For loop a constant number of times(i.e 10 times)
  • The Space Complexity of the above program is O(1), as we are running the program without using any data structure.

Program to Print ASCII value of A to Z using Type Conversion

#include <iostream>
using namespace std;

int main() {
    // Program to Print ASCII value of A to Z using type conversion
    string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(int i=0;i<str.length();i++){
        cout<<"ASCII value of "<<str[i]<<" is "<<(int)str[i]<<endl;
    }
    return 0;
}

Output:

ASCII value of A is 65
ASCII value of B is 66
ASCII value of C is 67
ASCII value of D is 68
ASCII value of E is 69
ASCII value of F is 70
ASCII value of G is 71
ASCII value of H is 72
ASCII value of I is 73
ASCII value of J is 74
ASCII value of K is 75
ASCII value of L is 76
ASCII value of M is 77
ASCII value of N is 78
ASCII value of O is 79
ASCII value of P is 80
ASCII value of Q is 81
ASCII value of R is 82
ASCII value of S is 83
ASCII value of T is 84
ASCII value of U is 85
ASCII value of V is 86
ASCII value of W is 87
ASCII value of X is 88
ASCII value of Y is 89
ASCII value of Z is 90

Working:

  • In this program, we create a string that includes all the uppercase alphabets from A to Z. We run a loop to iterate this string and print the alphabet along with its ASCII value.
  • Time Complexity:- O(n), where n(=26)is the length of the string.
  • Space Complexity:- O(n), where n(=26)is the length of the string.

Summary

  • The ASCII table is a collection of 256 symbols in the character set.
  • The regular ASCII code is 7 bits long and its range goes from 0 to 127.
  • The extended ASCII code is 8 bits long and its range goes from 128 to 255.
  • For each character, its ASCII value is unique.
  • The best way to find and print the ASCII values is through type conversion because we don’t need any prior knowledge about the ASCII values of the character.

Why is ASCII Table important?

ASCII is important because it acts as a link between the computer screen and computer hardware, and that link is now the same between all computers.

ASCII is mainly used as a translator, which translates machine text into human text and vice-versa. Machines only speak in binary language, a series of 0 and 1. However, just like two human languages which can use the same alphabet but have completely different words for similar objects, machines also had their own version of languages. ASCII is used as a method to give all machines the same language, allowing them to share important data in form of documents and files.ASCII Table is important because this development gave machines a common language.

Leave a Reply

Your email address will not be published. Required fields are marked *