2

So I have, let's say

float x;

and I have

 LPCWSTR message=L"X is";

how can I create a LPCWSTR with the message

"X is [x]"

?

3
  • Do you have access to the STL? You could use a wstringstream Commented May 31, 2012 at 13:42
  • @pstrjds: Pedantry -- streams are not part of the STL. They are part of the Standard Library. Commented May 31, 2012 at 14:21
  • @JohnDibling - I stand corrected. Thanks for pointing that out. Commented May 31, 2012 at 14:57

3 Answers 3

7

You could use wstringstream:

#include <string>
#include <sstream>
#include <iostream>

int main()
{
    float x = 0.1f;
    std::wstringstream s;
    s << L"X is " << x;
    std::wstring ws = s.str();
    std::wcout << ws << "\n";

    return 0;
}

and create an LPCWSTR from it if required, or just use the std::wstring.

Sign up to request clarification or add additional context in comments.

2 Comments

how would one create a LPCWSTR from the wstring?
@Ameoo, can you not just use ws.c_str() ?
5

You would use something like wsprintf() or its more modern (and safe) replacement, such as StringCbPrintf().

The point is that you can't just "convert", you need to build the string, character by character, that is the textual representation of the floating-point number.

1 Comment

Or you can use swprintf which is standard and in recent compilers takes a buffer size parameter so it's also safe.
0

How about using _vsnwprintf

headers

<wchar.h> <stdarg.h>

reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.