[英]C++ - cout flags work only for first output
When i pass some data, output is similiar to xxxxxxxx0x97 0x104 0x111 0x106 0x0
Why cout formatting is affecting only first output? 当我传递一些数据时,输出xxxxxxxx0x97 0x104 0x111 0x106 0x0
为什么cout格式仅影响第一个输出?
void Dumper::hex(const unsigned char * data, size_t len) {
cout << endl;
ios::fmtflags f(cout.flags());
/*
cout.fill('0');
cout.width(2);
*/
cout.fill('x');
cout.width(10);
for (int i = 0; i < len; i++) {
cout << "0x" << ((long)(data[i]) & 255) << " ";
}
cout.flags(f);
cout << endl;
}
The width()
is reset to 0 by any operation using it. 通过使用它的任何操作,将width()
重置为0。 The basic idea is that it is unlikely to apply to multiple fields. 基本思想是,它不太可能应用于多个字段。 In particular, it unlikely to apply to a value and a separator. 特别是,它不太可能应用于值和分隔符。 Thus, the width should be set up prior to each value. 因此,应在每个值之前设置宽度。
For you specific use I'd use std::internal
and std::showbase
. 对于您的特定用途,我将使用std::internal
和std::showbase
。 Also make sure use unsigned
values for your two digit hex values: otherwise you'll get the sign extended. 另外,请确保对两个数字的十六进制值使用unsigned
值:否则,您将得到扩展的符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.