ASCII codes
In this article, let’s explore ASCII codes.
- The American National Standards Institute(ANSI), which developed ANSI C, also developed the ASCII codes.
- ASCII stands for “American Standard Code for Information Interchange.”
- By using ASCII standard(that is, ASCII standard codes), you can encode 128 different characters which you encounter in your day-to-day life.
- That’s why to encode any ASCII character, you need 7 bits. Because there are 128 characters and one unique code will be used to represent a character.
ASCII table
To know the ASCII codes for the characters, you have to refer to the ASCII table. Figure 1 shows the ASCII table which I got from the internet.
You can see that ASCII table, A-Z are uppercase alphabets, and 65 to 90 are the respective ASCII codes in decimal.
a-z are the lower case letters, and 97 to 122 are the respective ASCII codes.
So, not only for English alphabets, but you can also get ASCII codes for special characters like space, double quotes, #, &, etc. You also get ASCII codes to represent numbers from 0 to 9. Also, you get ASCII codes to represent some special characters such as Carriage return, Backspace, Horizontal tab, etc.
These are the codes for the keys you find on your computer keyboard.
Example
To represent these characters(Figure 2) inside the computer memory.
For the machine, everything is a number. So, the machine doesn’t recognize it like ‘A’ or, ‘p’ or, ‘l’ or whatever. So, for the machine, everything is a number. Basically, we will store the respective ASCII codes of those characters inside the computer memory. So, that’s how we process the characters, strings, alphabets, etc.
Experience the magic of elfbar Canada vape, unlocking the essence of flavorful vaping with every puff. Discover satisfaction redefined.
Now let’s do one exercise to print the string Apple.
First, let’s represent the word Apple in our program. First of all, we know the ASCII codes(Figure 2). Now let’s use variables to store those ASCII codes.
Create a variable char a1. ‘a1’ means alphabet 1 and stores the ASCII code. ASCII code of ‘A’, that is 65.
char a2 (alphabet 2) ASCII code of ‘p’ is 112. So, char a3 = 112; char a4 = 108; char a5 = 101; char a6 = 58; char a7 = 41.
This procedure is very hard. We should refer to the ASCII table, get the code, and include that in the code. It is a very tedious process. There is one simple method. That is, instead of writing 65, write A and give single quotes. Write your alphabet or character inside the single quotes. So, the compiler will replace A by 65. So, that is easy. a1 holds not ‘A’, it holds 65. The code of ‘A’ so the compiler will do that in the background.
Let’s do that, instead of writing all the codes(Figure 4).
Let’s print that code using printf statement. Here, we want to print like characters, not like numbers. That’s why we have to use %c as the format specifier.
Let’s run the code. It printed character A, as shown in Figure 4.
When we use format specifier %d, it prints integers. It prints 65, as shown in Figure 5. 65 is a value stored in a1.
If we use %c, it prints the format will be changed to ASCII. In this exercise, we used seven format specifiers. The code is shown in Figure 6.
Let’s run and see the Output. It printed the string Apple:). So, that’s how you store the characters in your program.
All these codes can be replaced by one line by using arrays, like char a[ ] = “Apple:)”; For this, we have to explore arrays. This we will see when we cover the section arrays and strings.
So, that’s how you store the characters, and that’s how you print the characters. And hope you get the idea about ASCII and ASCII codes and the format specifier, %c.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1
Hands-On Rust Programming for Beginners