简体   繁体   English

十六进制 0x00 的 std::cout

[英]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## .我必须在标准输出上打印一些std::uint16_t值作为具有以下文本格式的十六进制: 0x## I found this code online which works fine for every value except 0 :我在网上找到了这段代码,它适用于除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 .由于某种原因,我不明白, 0被打印为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为了克服setw问题,这里有一些解决方法: “Permanent” std::setw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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