How can I append data to a std::string in hex format?

前端 未结 3 876
时光说笑
时光说笑 2020-12-31 02:18

I have an existing std::string and an int. I\'d like to concatenate the ASCII (string literal) hexadecimal representation of the integer to the

相关标签:
3条回答
  • 2020-12-31 02:29

    Building on xtofl's answer, the header you're looking for is <iomanip>. This is where std::hex, std::dec, and std::oct live, all of which can be directed into streams such that whatever gets sent into the stream after them is converted to that base.

    0 讨论(0)
  • 2020-12-31 02:31

    I believe 'string' only forward declares std::stringstream. So you also need to include:

    #include <sstream>
    
    0 讨论(0)
  • 2020-12-31 02:42

    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();
    
    0 讨论(0)
提交回复
热议问题