Printing numbers in hex format

This is a discussion on Printing numbers in hex format within the C++ Programming forums, part of the General Programming Boards category; I need to pass the std::cout to a function so that numbers print in hex and uppercase and 8 digits ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Printing numbers in hex format

    I need to pass the std::cout to a function so that numbers print in hex and uppercase and 8 digits are printed with 0 fill. I got it to print in hex and uppercase, but I can't figure out the 0 fill. It goes like

    Dump(std::cout);

    Dump() can't be changed though, so everything I want done has to be set for the stream I'm passing in.

    Any ideas?

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'm glad you said give ideas because that's all I got! lol.
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        // Outputting some ideas
        cout << "0x";
        cout.width(8);
        cout.fill('0');
        cout << "CFF" << endl;
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Thanks for replying. I can get it working like that, but I can't get it to set the stream like that and pass it off to a function.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Do a board search on displaying hexadecimal. There've been several threads discussing how to do it, and you should find an exaplanation and even some example source code for turning a number into a character string consisting of hex digits.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Thanks. I forgot to mention that I'll be printing unsigned long values, so I can't use a string of characters.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Well, let me back up then. I might not be understanding how the flags work. For instance, I was thinking this code would print 123 in hex format:

    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    
        std::cout.setf(std::ios::hex | std::ios::uppercase);
        std::cout << 123 << std::endl;
    
        std::cout << std::setiosflags(std::ios::hex | std::ios::uppercase);
        std::cout << 123 << std::endl;
    
        return 0;
    
    }
    But, it just prints 123 in the normal base 10 format. Where am I going wrong?

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    shouldnt he be using the hexadecimal format specifier %x ?
    When no one helps you out. Call google();

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The %x is for printf, since this is the C++ forum we are using stream insertion

    There is a state modifier called hex that is found in the std namespace. It works like so:
    Code:
    std::cout<<std::hex<<123<<std::endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 11:23 PM
  2. Finding and Printing prime numbers using arrays
    By Sektor in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2003, 08:29 PM
  3. Printing float numbers
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2003, 09:14 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. printing long hex
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-31-2002, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21