1. What is a character?
It is an image on the screen (I am talking about text screen and not graphics screen) of a symbol (a - z, A - Z, 0 - 9, punctuation marks like ! and others, special characters like $ and others) of the alphabet set of the English Language.
2. How can we save the image of the symbol 5 in computer memory?
Because a memory location holds 8-bit data (possible combination of 0 and 1), it has been decided that this bit/binary pattern: 00110101 would be stored for the image of 5. Why is this particular pattern is chosen for 5--this is another story? When this bit pattern is fed to an special electronics module, the image 5 appears on the text screen; when this bit pattern 00110110 is fed into that electronics module, the image of 6 appears on the text screen. These patterns are called ASCII Codes (American Standard Code for Information Interchange) for 5 and 6 respectively. The following Table of Fig-1 contains the ASCII codes for all possible printable characters.
3. What 'data type' is there to declare a variable (ch) that will hold the ASCII code of a character (say 5).
The data type is char. For example:
char ch = 0b00110101;
char ch = 0x35; //hex is compact form of binary and is used for convenience. Memory location always hold data in bit form.
char ch = 53;
char ch = '5';
4. What code/command should we use to direct the value of ch of Step-3 into the electronics module of Step-2 so that the image 5 appears on the text screen.
5. What will appear on the text screen if we choose data type byte for the example of Step-3 and execute the Serial.print() code?
byte ch = 0x35;
Now we need to discuss a little bit on the functional mechanism of print() method.
(1) The print() method checks the data type; if it is char, the print() method directs 0x35 to the electronics module of Step-2; as a result,the image 5 appears on the text screen.
(2) The print() method checks the data type; if it is byte, the print() method is transformed to the following form (this is my conceptual understanding):
Serial.print(ch, DEC); //DEC is for DECimal base = 10 base
The numerical value of ch (0x35) is converted into equivalent decimal value of 53 (3x161 + 5x160 = 48 + 5 = 53) and then the images of 5 and 3 are made to appear on the text screen by executing the following codes:
Serial.write(0x35); //to see 5 on screen, ASCII code of 5 (0x35) is to put to electronics module
Serial.write(0x33); //to see 5 on screen, ASCII code of 3 (0x33) is to put to electronics module
The above two codes could be written using print() methods in the following way:
Serial.print((char)0x35); //appears 5 ; known as casting to character
Serial.print((char)0x33); //appears 3
6. What are the differences among the following four declarations of Step-3?
char ch = 0b00000101;
char ch = 0x35;
char ch = 53;
char ch = '5'; //putting opening/closing single quote across a single character
There are no differences; they are the same. The declaration char ch = '5'; is friendly than these declarations: char ch = 0b00000101;, char ch = 0x35;, and char ch = 53;.
7. What is the meaning of the following declaration? (To what value the following expression would be evaluated?)
int num = (int) (ch - '0'); //assume char ch = 0x35
==> int num = (int) (0x35 -0x30); //'0' is evaluated to 0x30 as per Step-6
==> int num = (int)(0x05);
ch-'0' is evaluated to 0x05 = 00000101, and it is 8-bit. The destination variable num has been declared as a 16-bit variable; so, we need to append 8 zeros to the left of 0x05 to make it 16-bit (00000000 00000101). This is dine by a process called casting, and it is done by putting the keyword int surrounded by pair of parentheses before 0x05.