home > core-concept > standard

What on earth is ASCII?_

ASCII (American Standard Code for Information Interchange) pronounced as "as-key", is the most common character encoding format for text data in computers and on the Internet. ASCII assigns standard numeric values to letters, numerals, punctuation marks, control codes, and other characters used in computers.

Before ASCII was developed, different makes and models of computers could not communicate with one another. Each computer manufacturer represented alphabets, numerals, and other characters in their own way. IBM (International Business Machines Corporation) alone used 9 different character sets. So imagine a world where your MacBook couldn’t send a message to your friend’s HP Pavilion. In 1961 Bob Bemer of IBM submitted a proposal to the American National Standards Institute (ANSI) for a common computer code. The American National Standards Institute first published it as a standard for computing in 1963.

ASCII was originally developed for teleprinters or teletypewriters, but it eventually found wide application in personal computers (PCs), beginning with IBM’s first PC, in 1981. Hence why it contains non-printing control characters that were originally intended for use with teletype printing terminals.

How does ASII work?

ASCII offers a universally accepted and understood character set for basic data communications. It enables developers to design interfaces that both humans and computers understand. ASCII codes a string of data as ASCII characters that can be interpreted and displayed as readable plain text for people and as data for computers.

Computers store data using binary numbers - i.e., numbers consisting of various sequences of 0’s and 1’s so data encodings like ASCII are useful for storing text data in this numeric form. As everything on your computer trickles down to 0s and 1s, ASCII was developed to use seven-digit binary numbers. Since there are 128 different possible combinations of seven 0’s and 1’s, the code can represent 128 different characters. For example, the character ‘A’ is represented by 1000001. As binary is a numeral system(base-2) ASCII characters may also be represented in the following ways:

  • as decimal numbers(the base-10 numbers you use day-to-day) from 0 to 127; or
  • as pairs of hexadecimal digits - base-16 numbers, represented as 0 through 9 and A through F for the decimal values of 10-15; or
  • as three-digit octal (base-8) numbers.

The table below displays ASCII encodings for some commonly used characters:

Character Decimal Hexadecimal Octal Binary (7 bit) Binary (8 bit)
A 65 41 101 1000001 01000001
B 66 42 102 1000010 01000010
S 83 53 123 1010011 01010011
a 97 61 141 1100001 01100001
b 98 62 142 1100010 01100010
s 115 73 163 1110011 01110011
; (semicolon) 59 3B 073 0111011 00111011
! 33 21 041 0100001 00100001

ASCII characters were initially encoded into 7 bits and stored as 8-bit characters with the most significant bit, the left-most bit, set to 0. You can view a full ASCII Table at asciitable.com.

As you can see ASCII is not only a universally accepted encoding format but is a compact character encoding system. Standard codes can be expressed in 7 bits which means data that can be expressed in the standard ASCII character set requires only as many bytes to store or send as the number of characters in the data.

Programming with ASCII

The ASCII character codes for letters and numbers are well adapted to programming techniques for manipulating text and using numbers for calculations or storage as raw data. This makes ASCII efficient for programming. In this section, we will explore how to determine the ASCII value of a character using Java and Python.

Python

We can use the ord() function to compute the ASCII code of a character and the chr() function to convert a decimal ASCII code into a character. For example:

ch_A = 'A'
ch_B = 'B'
ch_S = 'S'

print("%s's ASCII value is" % ch_A, ord(ch_A))
print("%s's ASCII value is" % ch_B, ord(ch_B))
print("%s's ASCII value is" % ch_S, ord(ch_S))

asciiVal = 59
print(asciiVal, "is the ASCII value of", chr(asciiVal))

As always, light work in Python! Let's view the output of the program:

A's ASCII value is 65
B's ASCII value is 66
S's ASCII value is 83
59 is the ASCII value of ;

Java

Java and C are interesting when it comes to ASCII as the ASCII conversion behaviour is built into how char and int interact. You can make use of casting to cast a char into an int to determine the ASCII code of the character or cast an int into a char to convert a decimal ASCII code into a character:

public class ASCII {
    public static void main(String[] args) {
        char chA = 'A';
        char chB = 'B';
        char chS = 'S';

        // convert the characters to integers to obtain the decimal
        // ASCII value
        int asciiValA = (int) chA;
        int asciiValB = (int) chB;
        int asciiValS = (int) chS;

        System.out.println(chA + "'s ASCII value is " + asciiValA);
        System.out.println(chB + "'s ASCII value is " + asciiValB);
        System.out.println(chS + "'s ASCII value is " + asciiValS);
        
        int asciiVal = 59;
        char chCheck = (char) asciiVal;
        System.out.println(asciiVal + " is the ASCII value of " + chCheck);
    }
}

For once, even lighter work in Java! Let's view the output of the program:

A's ASCII value is 65
B's ASCII value is 66
S's ASCII value is 83
59 is the ASCII value of ;

You can extend the code shown in this section by adding conditional statements to tackle problems like:

  • Determine if a letter is uppercase or lowercase.
  • Determine if a character is alphanumeric.

C

Just as with Java the ASCII conversion behaviour is built into how char and int interact. You can make use of casting to cast a char into an int to determine the ASCII code of the character or cast an int into a char to convert a decimal ASCII code into a character:

#include <stdio.h>

int main() {
    char chA = 'A';
    char chB = 'B';
    char chS = 'S';

    int asciiValA = (int) chA;
    int asciiValB = (int) chB;
    int asciiValS = (int) chS;

    printf("%c's ASCII value is %d\n", chA, asciiValA);
    printf("%c's ASCII value is %d\n", chB, asciiValB);
    printf("%c's ASCII value is %d\n", chS, asciiValS);

    int asciiVal = 59;
    char chCheck = (char) asciiVal;
    printf("%d is the ASCII value of %c\n", asciiVal, chCheck);

    return 0;    
}

As you can see, it similar to Java, the output of the program is:

A's ASCII value is 65
B's ASCII value is 66
S's ASCII value is 83
59 is the ASCII value of ;

Maths magic with Java and C

ASCII Table: Numbers

Character Decimal Hexadecimal Octal Binary (7 bit) Binary (8 bit)
0 48 30 060 0110000 00110000
1 49 31 061 0110001 00110001
2 50 32 062 0110010 00110010
3 51 33 063 0110011 00110011
4 52 34 064 0110100 00110100
5 53 35 065 0110101 00110101
6 54 36 066 0110110 00110110
7 55 37 067 0110111 00110111
8 56 38 070 0111000 00111000
9 57 39 071 0111001 00111001

Notice how all the numbers are in the same section in the ASCII table. This observation is going to support our magic.

The maths magic

As in Java and C, the conversion behaviour is built into how char and int interact we can convert a given character number into its integer value as follows:

char ch = '1';
int numericValueOfCh = ch - '0';

Here we subtract the ASCII value of 0 (which is 48) from the ASCII value of 1 (which is 49) to get the desired value of 1. Cool stuff right?

This magic trick works for any number because the ASCII values of numbers are in the same section in the ASCII table in ascending order.

An example in C

#include <stdio.h>

int main() {
    char numbers[] = {
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9'
    };
    int numericVal;
    int ch;

    for (int i = 0; i < 10; i++) {
        ch = numbers[i];
        numericVal = ch - '0';
        printf("The character '%c' has a numeric value is %d\n", ch, numericVal);
    }

    return 0;    
}

The output of the program:

The character '0' has a numeric value is 0
The character '1' has a numeric value is 1
The character '2' has a numeric value is 2
The character '3' has a numeric value is 3
The character '4' has a numeric value is 4
The character '5' has a numeric value is 5
The character '6' has a numeric value is 6
The character '7' has a numeric value is 7
The character '8' has a numeric value is 8
The character '9' has a numeric value is 9

Just like that, with one line of code, you can convert any character number into its true numeric value.

Note: Casting the char to int gives you the ASCII value, so for '1' you will get 49, not 1. That's why you have to subtract '0'.

ASCII Today

Though ASCII has been extended to support 255 distinct characters, the characters in a standard character set are enough for English language communications. But even with the diacritical marks and Greek letters supported in extended ASCII, it is difficult to accommodate languages that do not use the Latin alphabet. Consider Mandarin or Arabic? Also, though standard ASCII encoding is efficient for English language and numerical data, representing characters from other alphabets requires more overhead such as escape codes.

Given these issues, Modern computer systems have evolved to use Unicode, which has millions of code points, but the first 128 of these are the same as the ASCII set.

table of contents

Ready to join the movement?

Dive into our articles, explore our resources, and join the clan. Together, let's rewrite the rules of coding education and prove that learning can be fun, effective, and a little bit rebellious.

Subscribe to receive updates about new posts.