How do I construct a std::string from a DWORD?

前端 未结 8 2226
死守一世寂寞
死守一世寂寞 2020-12-07 00:30

I have following code:

Tools::Logger.Log(string(GetLastError()), Error);

GetLastError() returns a DWORD a numeric

相关标签:
8条回答
  • 2020-12-07 01:24

    You want to convert the number to a string:

    std::ostringstream os;
    os << GetLastError();
    Log(os.str(), Error);
    

    Or boost::lexical_cast:

    Log(boost::lexical_cast<std::string>(GetLastError()), Error);
    
    0 讨论(0)
  • 2020-12-07 01:24

    Since C++11

    std::to_string() with overloads for int, long, long long, unsigned int, unsigned long, unsigned long long, float, double, and long double.

    auto i = 1337;
    auto si = std::to_string(i); // "1337"
    auto f = .1234f;
    auto sf = std::to_string(f); // "0.123400"
    

    Yes, I'm a fan of auto.

    To use your example:

    Tools::Logger.Log(std::to_string(GetLastError()), Error);
    
    0 讨论(0)
提交回复
热议问题