使用 C++ 将数组的十六进制值打印为字符串
使用标准 C++ I/O(例如 std::cout
),是否可以将数组的值(无论多长)“打印”到字符串中?
例如,假设我有以下数组:
unsigned long C = {0x497fecf2, 0xfa989ea3, 0xd594974e};
我希望能够将这些值打印到字符串中,然后从中删除“0x
”。从另一个 SO 问题,我发现了如何使用 cout< 打印十六进制值/代码>。
我所问的是否可能如我所描述的那样?
回到旧的 comp sci 分配进行基数转换,并将十进制值转换为十六进制,使用查找表将适当的下一个十六进制添加到字符串中,会更好吗?
Using standard C++ I/O (such as std::cout
), is it possible to "print" the value of an array (however long) into a string?
For example, say I have the following array:
unsigned long C = {0x497fecf2, 0xfa989ea3, 0xd594974e};
I'd like to be able to print those values into a string, and then remove the "0x
" from them. From another SO question, I've found how to print hex values with cout
.
Is what I'm asking possible the way I've described?
Would it be better to just go back to an old comp sci assignment for base conversions, and convert the decimal value into hex, using a lookup table to add the appropriate next hexit to the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个
std::ostringstream
,并将它们打印到其中,就像cout
一样。使用字符串流的str()
成员检索包含内容的字符串。Create a
std::ostringstream
, and print them to it just like you could tocout
. Retrieve the string with the contents with the stringstream'sstr()
member.