nyarlathotep108
nyarlathotep108

Reputation: 5523

std::cout of the hexadecimal 0x00

I've to print on the standard output some std::uint16_t values as hexadecimal with the following text formatting: 0x##. I found this code online which works fine for every value except 0:

std::cout << std::internal 
          << std::setfill( '0' ) 
          << std::hex 
          << std::showbase 
          << std::setw( 4 ) 
          << value << std::endl;

For some reason I don't understand, 0 is printed as 0000. All the other values are correctly printed as expected.

Upvotes: 2

Views: 1630

Answers (1)

MagunRa
MagunRa

Reputation: 563

For your additional question. You only need to set the width again. The rest of the manipulators are persistent.

std::cout << std::internal 
      << std::setfill( '0' ) 
      << std::hex 
      << std::showbase ;

for(std::uint16_t i =1;i<255;++i){
      std::cout<< std::setw( 4 )<<i<<"\n";
}

To overcome the setw issue here are some workarounds: “Permanent” std::setw

Upvotes: 1

Related Questions