将积分作为二进制输出到 ostringstream?
我刚刚意识到可以使用位集根据位集的(固定)大小将二进制数据输出到流。使用积分将二进制数据输出到流的最少额外语法的方法是什么?
为了说明我的意思,这是一个程序及其输出。我希望该程序的第二行输出与第一行相同,但不诉诸用于输出第三行的技术。
int main()
{
ostringstream bsout, uout, xout;
bitset<32> bs (0x31323334);
unsigned u = 0x31323334;
bsout << bs;
cout << bsout.str() << endl;
uout << u;
cout << uout.str() << endl;
xout << bitset<32>(u);
cout << xout.str() << endl;
return 0;
}
00110001001100100011001100110100
825373492
00110001001100100011001100110100
I just realized that one can use bitset to output binary data to a stream based on the (fixed) size of the bitset. What's the least-extra-syntax way to output binary data to a stream using integrals?
To show what I mean, here's a program and its output. I'd like the second line of output from this program to be identical to the first line but without resorting to the technique used to output the third line.
int main()
{
ostringstream bsout, uout, xout;
bitset<32> bs (0x31323334);
unsigned u = 0x31323334;
bsout << bs;
cout << bsout.str() << endl;
uout << u;
cout << uout.str() << endl;
xout << bitset<32>(u);
cout << xout.str() << endl;
return 0;
}
00110001001100100011001100110100
825373492
00110001001100100011001100110100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,没有类似于 oct 和 hex 的 std::bin 操纵器。通过
bitset
对象进行过滤是输入和输出二进制格式数字的首选方法。Unfortunately, there is no
std::bin
manipulator akin tooct
andhex
. Filtering through abitset
object is the preferred method for input and output of binary-formatted numbers.