# print hex value of character

• 09-10-2008
Abda92
print hex value of character
Hi everyone,
Sorry for the noobish question, but I just started learning C++ (I learned C before) and got stuck on a really simple exercise:
Write a program that prints out the letters ´a ´..´z ´ and the digits ´0 ´..´9 ´ and their
integer values. Do the same for other printable characters. Do the same again but use hexadecimal notation.

Here's what I got so far:
Code:

```int main() {         cout << "a\t" << int('a') << endl                 << "b\t" << int('b') << endl                 << "c\t" << int('c') << endl                 << "d\t" << int('d') << endl                 << "e\t" << int('e') << endl                 << "f\t" << int('f') << endl                 << "g\t" << int('g') << endl                 << "h\t" << int('h') << endl                 << "i\t" << int('i') << endl                 << "j\t" << int('j') << endl                 << "k\t" << int('k') << endl                 << "l\t" << int('l') << endl                 << "m\t" << int('m') << endl                 << "n\t" << int('n') << endl                 << "o\t" << int('o') << endl                 << "p\t" << int('p') << endl                 << "q\t" << int('q') << endl                 << "r\t" << int('r') << endl                 << "s\t" << int('s') << endl                 << "t\t" << int('t') << endl                 << "u\t" << int('u') << endl                 << "v\t" << int('v') << endl                 << "w\t" << int('w') << endl                 << "x\t" << int('x') << endl                 << "y\t" << int('y') << endl                 << "z\t" << int('z') << endl                 << "0\t" << int('0') << endl                 << "1\t" << int('1') << endl                 << "2\t" << int('2') << endl                 << "3\t" << int('3') << endl                 << "4\t" << int('4') << endl                 << "5\t" << int('5') << endl                 << "6\t" << int('6') << endl                 << "7\t" << int('7') << endl                 << "8\t" << int('8') << endl                 << "9\t" << int('9') << endl                 << "{\t" << int('{') << endl                 << ".\t" << int('.') << endl                 << "+\t" << int('+') << endl;         return 0; }```
I know I could easily use a few loops and make it much shorter, but since the book didn't cover loops yet I just kept it expanded.
Now my problem is, I don't know how to output the character's hexadecimal value. Could someone point me out to the right direction? I searched google but I didn't find anything simple,
• 09-10-2008
laserlight
Read this page on format flags and you will find something useful.
• 09-10-2008
Abda92
Thanks laserlight. Got it :)
• 09-10-2008
matsp
And of course, you could quite easily do this with loops instead of listing all the characters on a line each. That would take the program from about 40 lines to about 10.

--
Mats
• 09-10-2008
Abda92
Yes I know that. But I'm currently learning the language from "The C++ Programming Language", and I haven't reached loops yet (although I know already how to use them). In addition to that, the author of the book recommends you don't assume which character set your system uses.
Quote:

It is not safe to
assume that there are no more than 127 characters in an 8bit
character set (e.g., some sets provide
255 characters), that there are no more alphabetic characters than English provides (most European languages provide more), that the alphabetic characters are contiguous (EBCDIC leaves a gap between &#180;i&#180; and &#180;j&#180;)...
That's why I preferred to just keep it expanded with no loops ;)
• 09-10-2008
matsp
Good point. If you want a solution that works with loops, but doesn't rely on the character set used being in any particular order, then you could do this:
Code:

```int main() {   int i;   char chars[] = "abcdefghijklmnoprstuvwxyz1234567890{+-.";   for(i = 0; i < sizeof(chars)-1; i++)   {       std::cout << chars[i] << "\t" << static_cast<int>(chars[i]) << std::endl;   }   return 0; }```
--
Mats
• 09-10-2008
Abda92
Interesting.. You always amaze me! I never thought of anything like that.