Thread: cout format as hex

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    cout format as hex

    I've been slowly working my way through C++ for Dummies 5th Edition and I have met a slight predicament.

    The book told me to use
    Code:
    cout.setf(cout.hex);
    to format the output as hex, but instead it printed the following

    Code:
    iArg1        = 0x4660
    iArg2        = 0x255
    ~iArg1       = 0x4294962635
    ~iArg2       = 0x4294967040
    iArg1 & iArg2 = 0x52
    iArg1 | iArg2 = 0x4863
    iArg1 ^ iArg2 = 0x4811
    By a bit of googling I found that slipping in
    Code:
    cout << hex;
    instead of cout.setf(cout.hex);, the program would format the output in the format the book had shown.

    Code:
    iArg1        = 0x1234
    iArg2        = 0xff
    ~iArg1       = 0xffffedcb
    ~iArg2       = 0xffffff00
    iArg1 & iArg2 = 0x34
    iArg1 | iArg2 = 0x12ff
    iArg1 ^ iArg2 = 0x12cb
    I just wanted to ask if it was ok using "cout << hex;" as I would hate to develop any nasty habits this early on that would cause me problems later.

    Also, would anyone have any idea why the method the book used didnt work? I even copied the code from the CD (I know thats the worst thing i could do while learning, but i was desperate ) to make sure i had not made any errors.

    I do apologise if this is a simple problem, making the thread a waste of time for anyone that reads it, but i googled and searched these forums and couldnt find a thing .

    I also feel bad for my first post being a post asking for help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    cout.setf(ios::hex);
    Quote Originally Posted by RiPC0rz
    I just wanted to ask if it was ok using "cout << hex;" as I would hate to develop any nasty habits this early on that would cause me problems later.
    Using it is fine.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Sorry, I forgot to mention I tried
    Code:
    cout.setf(ios::hex);
    It unfortunately did not remedy the problem either. Does using "cout.hex" and "ios::hex" make a difference?

    I am starting to get the feeling I made a wrong choice in which book to start with, if i've been using the wrong syntax so far.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Oops, forgot something, try:
    Code:
    cout.setf(ios_base::hex,ios_base::basefield);
    Usually I use the stream manipulators in <iomanip> instead of the setf stream member function. I find it involves less typing. To each his own.
    Last edited by hk_mp5kpdw; 02-20-2008 at 12:34 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    That done the trick, cheers mate. I really don't understand it at the minute, but i'll have to make a note to come back to that when i've progressed a bit.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Spying" on cout
    By sirjis in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2006, 05:00 PM
  2. converting an int into hex and memcpy
    By puppy in forum C Programming
    Replies: 10
    Last Post: 07-03-2006, 11:39 AM
  3. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  4. Hex to binary conversion and storing in nybble
    By shoobsie in forum C Programming
    Replies: 14
    Last Post: 10-25-2005, 02:51 AM
  5. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM