Hey guys! This is just a tip on how you can get the ascii of a char. Well, luckily, C++ provides you with a built-in function that gives you the ASCII of a character so that you don't have to lookup into an ASCII table. 😉
Let, you have a char.
char x = 'A';
To get its ascii into an int :
int n = (int) x; //'n' now contains 65 that is the ASCII of 'A'
You can direct print it as well, e.g.
cout << (int) x;
You can try the same thing with the individual elements of a char array or a string.
string str = "ABC";
cout << (int) str[0];
cout << (int) str[1];
Stay tuned for more shortcuts! 😎
Thank you for reading! 😊
0 comments:
Post a Comment