如何将数据附加到std :: string的十六进制格式? [英] How can I append data to a std::string in hex format?

查看:1502
本文介绍了如何将数据附加到std :: string的十六进制格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的std :: string和一个int。我想将int附加到字符串,但是以人类可读的形式(十六进制符号),而不是二进制的gibberish。

I have an existing std::string and an int. I'd like to append the int to the string, but in human readable form (hex notation), rather than in binary gibberish.

通常,我只使用printf,但是我不能用std :: string(我可以吗?)

Normally, I'd just use printf, but I can't do this with a std:: string (can I?)

有关如何做的建议吗?

Example: 
Given:   
   std::string - "Your Id Number is: "
   int - 0xdeadc0de
Output:   
   std::string - "Your Id Number is: 0xdeadc0de"


推荐答案

使用字符串流。你可以使用它作为任何其他输出流,所以你可以同样插入 std :: hex 。然后提取 stringstream :: str()函数。

Use a stringstream. You can use it as any other output stream, so you can equally insert std::hex into it. Then extract it's stringstream::str() function.

std::stringstream ss;
ss << "your id is " << std::hex << 0x0daffa0;
const std::string s = ss.str();

这篇关于如何将数据附加到std :: string的十六进制格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆