Thread: print hex value of character

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    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,
    I might not be a pro, but I'm usually right

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read this page on format flags and you will find something useful.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Thanks laserlight. Got it
    I might not be a pro, but I'm usually right

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    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.
    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
    I might not be a pro, but I'm usually right

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Interesting.. You always amaze me! I never thought of anything like that.
    I might not be a pro, but I'm usually right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex dump
    By Banana Man in forum C Programming
    Replies: 17
    Last Post: 01-06-2008, 11:03 AM
  2. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM