formatting (std::cout) preferences

Posted on 2009-04-19
Last Modified: 2012-05-06
The source below outputs:
0: 0xDC 0xDC 0xDC 0xDC
4: 0xDC 0xDC 0xDC 0xDC
8: 0xDC 0xDC

For a 32 bit integer, my desire is:
0: 0x000000DC 0x000000DC 0x000000DC 0x000000DC
4: 0x000000DC 0x000000DC 0x000000DC 0x000000DC
8: 0x000000DC 0x000000DC

For a 16 bit integer (unsigned short on my implementation) my desire is:
0: 0x00DC 0x00DC 0x00DC 0x00DC
4: 0x00DC 0x00DC 0x00DC 0x00DC
8: 0x00DC 0x00DC

I'd like to modify source below to achieve this.  In addition, I'd like some flexibility on the number of elements in a row.  For instance (assume unsigned short), I could also have:

0: 0x00DC 0x00DC 0x00DC 0x00DC 0x00DC
5: 0x00DC 0x00DC 0x00DC 0x00DC 0x00DC

# include <iostream>

# include <string>

# include <vector>

# include <iomanip>

# include <limits>

typedef std::vector < int > INT_VEC ;

int main() { 

  INT_VEC ivv ( 10 ) ; 

  std::cout.setf( std::ios::hex, std::ios::basefield); 

  std::cout.setf( std::ios::uppercase ); 

  std::setw ( 32 ) ; 

  std::fill ( ivv.begin(), ivv.end(), 0xDC ) ;

  bool disp_index  = true; 

  for ( int odx ( 0 ); odx < ivv.size(); ++odx  )


    if ( odx && ( odx % 4 == 0 )  ) 


      std::cout << std::endl;

      disp_index = true;


    if ( disp_index ) {

      disp_index = false;

      std::cout << odx << ": ";


    std::cout << std::hex <<  "0x" << ivv [ odx ] << " " ;


  std::cin.get() ; 


Open in new window

Question by:forums_mp
    LVL 53

    Accepted Solution

    You can use setw to set the minimum width of the output field. For example, use setw(8) if you want to show a 32bit value in hexadecimal.

    You can use setfill to set the fill character. For example, use setfill('0') if you want to padd the value with 0's.

    uint32_t value;
    cout << "0x" << setfill('0') << setw(8) << hex << value;

    Open in new window

    LVL 39

    Assisted Solution

    >>>> In addition, I'd like some flexibility on the number of elements in a row.  

    Use variables in the calls to

               cout << setw(hexWidth) << ...

    and for

           if ( odx && ( odx % wrapCount == 0 )  )

    and set hexWidth to 4 and wrapCount to 5 to get the required output.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now