2

I want to convert a DWORD value to wstring value.

DWORD dw;
wstring wstr(L"");
dw = 2;

Can you suggest ideal way to assign 'dw' to 'dwstr' ?

Rajasekhar
  • 894
  • 5
  • 13
  • 25

3 Answers3

13

Use std::to_wstring:

std::wstring wstr = std::to_wstring(dw);
chris
  • 60,560
  • 13
  • 143
  • 205
  • Hi Chris, If I use wstr = std::to_wstring(dw), I am getting error as "more than one overload function matches for the arguments". Please suggest to rectify the error. – Rajasekhar Jun 12 '13 at 06:15
  • 1
    @Rajasekhar, Try explicitly casting it to an `unsigned long long` IIRC. It used to only have a couple of overloads instead of all of them. – chris Jun 12 '13 at 06:16
  • Will it cause any problem, if we cast from "unsigned long" to "unsigned long long" ? – Rajasekhar Jun 12 '13 at 06:24
  • 3
    @Rajasekhar Promotion from unsigned long to unsigned long long does not result in any loss of information. What sort of problems are you thinking of? (It sounds like you're having more fundamental issues with the C++ language. You might want to familiarize yourself with the type system.) – Raymond Chen Jun 12 '13 at 06:25
  • Thanks Raymond. I am new to C++ and I am developing applications. – Rajasekhar Jun 12 '13 at 06:27
2

If you're using an older compiler that doesn't have std::to_wstring, consider lexical_cast (e.g., from Boost) instead.

As an aside, if you want to create wstr as an empty string, just wstring wstr; suffices.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

On C++03 you can do:

std::wstringstream stream;
stream << dw;
wstr = stream.str();
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147