C++ 将 int 转换为 const wchar_t*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15109469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert int to const wchar_t*
提问by Kira
As the title indicates I want to know the best way to convert an intto a const wchar_t*.
in fact I want to use the _tcscpyfunction
正如标题所示,我想知道将 an 转换int为 a的最佳方法const wchar_t*。其实我想用这个_tcscpy功能
_tcscpy(m_reportFileName, myIntValue);
采纳答案by Mr.C64
Since you are using a C approach(I'm assuming that m_reportFileNameis a rawC wchar_tarray), you may want to consider just swprintf_s()directly:
由于您使用的是C 方法(我假设这m_reportFileName是一个原始Cwchar_t数组),您可能只想swprintf_s()直接考虑:
#include <stdio.h> // for swprintf_s, wprintf
int main()
{
int myIntValue = 20;
wchar_t m_reportFileName[256];
swprintf_s(m_reportFileName, L"%d", myIntValue);
wprintf(L"%s\n", m_reportFileName);
}
In a more modern C++ approach, you may consider using std::wstringinstead of the raw wchar_tarray and std::to_wstringfor the conversion.
在更现代的C++ 方法中,您可以考虑使用std::wstring代替原始wchar_t数组并std::to_wstring进行转换。
回答by Peter Wood
In C++11:
在C++11:
wstring value = to_wstring(100);
Pre-C++11:
预C++11:
wostringstream wss;
wss << 100;
wstring value = wss.str();
回答by unwind
That's not a "conversion". It's a two-step process:
这不是“转换”。这是一个两步过程:
- Format the number into a string, using wide characters.
- Use the address of the string in the call.
- 使用宽字符将数字格式化为字符串。
- 在调用中使用字符串的地址。
The first thing can be accomplished using e.g. StringCbPrintf(), assuming you're building with wide characters enabled.
StringCbPrintf()假设您在构建时启用了宽字符,则可以使用 eg 来完成第一件事。
Of course, you can opt to format the string straight into m_reportFileName, removing the need to do the copy altogether.
当然,您可以选择将字符串直接格式化为m_reportFileName,从而完全不需要进行复制。

