简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM