formatting (std::cout) preferences

Posted on 2009-04-19
Medium Priority
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

Infinity08 earned 120 total points
ID: 24182129
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

itsmeandnobodyelse earned 80 total points
ID: 24183209
>>>> 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 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