The char data type is one of the major data types in all programming languages, including int, float, Boolean, and String. If you have used the char data type before but are curious about how char relates to integers, then this article is for you. During the course of this article, I will introduce you to the ASCII table, converting char to int and vice versa. I know we can easily convert a char or string to a number but only if the char or string are numbers themselves, but what if I tell you, you can convert the letter "A" to 65 🤯.
The ASCII tableASCII (American Standard Code for Information Interchange) is a widely-used character encoding standard, assigning unique numeric codes to represent characters, symbols, and control characters. This basically means that every character(including alphabets), symbols and control characters have a particular number that represents them. In the case of the letter "A", the number is 65. Here is an image of the ASCII table.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char alphabet=65;
Serial.println(alphabet);
}
void loop() {
// put your main code here, to run repeatedly:
}
// output will be the letter A
The code above basically outputs the ASCII equivalent of the integer "65" which is the letter "A". if you check the table for the number 65 you will see that it corresponds to the letter "A". You can try this for other characters on the table.
char to intvoid setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char alphabet='%';
Serial.println(int(alphabet));
}
void loop() {
// put your main code here, to run repeatedly:
}
The code above converts our char "%" to its corresponding integer. Now let's do something cool with what we have learnt.
Generate the AlphabetTo do this we will basically just use a loop to loop through numbers from 65 to 90 and print out the result on the serial monitor.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
for(char i=65; i<=90; i++){
// i represents our alphabets
Serial.println(i);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
The code above will print out all the letters of the alphabet in upper case.
Now why don't you try printing them in lowercase? Just note their numeric representative and you are good to go.
Generate the whole ASCII tableThe ASCII table I provided has numbers from 0-127. so we are basically repeating what we did earlier but starting from 0 and ending at 127
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char i=0;
int a =0;
for(i=0,a=0; i<=127,a<=127; i++,a++){
// i represents our alphabets
// char buffer[1000];
Serial.print(a);
Serial.print("=");
Serial.println(i);
//sprintf(buffer, "%d %s",a,i);
// Serial.println(buffer);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
This printed out the whole ASCII table but some parts of the table were left blank, it is probably because Arduino can't print such characters. In the code above you would notice that I tried using "sprint" but I couldn't get it to work, all the characters were just blank, I even increased the buffer size to 10000, but still nothing. If you are able to figure it out, kindly paste your solution in the comment section so I can learn from you guys.
ConclusionOverall, we learnt about the ASCII table, which is a table containing the numerical representatives of all characters and we also learned how to go from char to int and from int to char.
Thank you so much for reading.
Kind Regards
PIUS
Comments