将float转换为LPCWSTR

Cos*_*inO 2 c++ winapi

所以我有,让我们说

float x;
Run Code Online (Sandbox Code Playgroud)

我有

 LPCWSTR message=L"X is";
Run Code Online (Sandbox Code Playgroud)

如何使用消息创建LPCWSTR

"X是[x]"

hmj*_*mjd 7

你可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

LPCWSTR根据需要创建一个,或者只使用std::wstring.

  • @Ameoo,你能不能只使用`ws.c_str()`? (3认同)

unw*_*ind 5

您可以使用类似wsprintf()或更现代(和安全)替代的东西,例如StringCbPrintf().

关键是你不能只是"转换",你需要逐个字符地构建字符串,这是浮点数的文本表示.