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

Weidling Christopd picture Weidling Christopd · May 21, 2009 · Viewed 38.2k times · Source

I have following code:

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

GetLastError() returns a DWORD a numeric value, but the constructor of std::string doesn't accept a DWORD.

What can I do?

Answer

Doug T. picture Doug T. · May 21, 2009

You want to read up on ostringstream:

#include <sstream>
#include <string>

int main()
{
   std::ostringstream stream;
   int i = 5;
   stream << i;
   std::string str = stream.str();
}