All about C plus plus algorithms!

Sunday 20 November 2016

Get ASCII of a char || getASCII in C++ || C plus plus



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! 😊




Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive

Copyright by progrithms.blogspot.com. All Rights Reserved.. Powered by Blogger.

Copyright © Programs and Algorithms | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com