C++ 如何将 int 转换为 LPCTSTR?赢32
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7184698/
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
How do I convert a int to LPCTSTR? Win32
提问by Nick
I would like to display an int value in a win32 MessageBox. I have read some different methods to perform this cast. Can someone provide me with a good implementation.
我想在 win32 MessageBox 中显示一个 int 值。我已经阅读了一些不同的方法来执行这个转换。有人可以为我提供一个很好的实现。
New to Win32 programming so go easy :)
Win32 编程的新手,所以很容易:)
update
更新
So this is what i have so far. It works.. but the text looks like Chinese or some other double byte characters. I am not groking the Unicode vs. not Unicode types. Can someone help me understand where I am going wrong?
所以这就是我到目前为止所拥有的。它有效......但文本看起来像中文或其他一些双字节字符。我不是在探索 Unicode 与非 Unicode 类型。有人可以帮助我了解我哪里出错了吗?
int volumeLevel = 6;
std::stringstream os;
os<<volumeLevel;
std::string intString = os.str();
MessageBox(plugin.hwndParent,(LPCTSTR)intString.c_str(), L"", MB_OK);
采纳答案by In silico
LPCTSTR
is defined like this:
LPCTSTR
定义如下:
#ifdef UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif
std::string::c_str()
returns a const char*
only. You can't convert a const char*
directly to const wchar_t*
. Normally the compiler will complain about it, but with the LPCTSTR
cast you end up forcing the compiler to shut up about it. So of course it doesn't work as you expect at runtime. To build on what you have in your question, what you probably want is something like this:
std::string::c_str()
const char*
只返回一个。您不能将 aconst char*
直接转换为const wchar_t*
. 通常编译器会抱怨它,但是通过LPCTSTR
演员表你最终会迫使编译器闭嘴。所以当然它在运行时不会像你期望的那样工作。为了建立在你的问题的基础上,你可能想要的是这样的:
// See Felix Dombek's comment under OP's question.
#ifdef UNICODE
typedef std::wostringstream tstringstream;
#else
typedef std::ostringstream tstringstream;
#endif
int volumeLevel = 6;
tstringstream stros;
stros << volumeLevel;
::MessageBox(plugin.hwndParent, stros.str().c_str(), L"", MB_OK);
回答by Mahmut EFE
Converting for MFC like belov :
像 belov 一样转换为 MFC :
int number = 1;
CString t;
t.Format(_T("%d"), number);
AfxMessageBox(t);
I used and it worked for me.
我用过,它对我有用。
回答by Hyman
nA few ways:
n几种方式:
int value = 42;
TCHAR buf[32];
_itot(value, buf, 10);
another more friendly way for your case:
对您的情况更友好的另一种方式:
int value = 42;
const size_t buflen = 100;
TCHAR buf[buflen];
_sntprintf(buf, buflen - 1, _T("the value is %d"), value);
回答by user3697878
int OurVariable;
LPCWSTR result=(to_string(OurVariable).c_str());
or
或者
LPCWSTR result=LPCSTR(to_string(OurVariable).c_str());
or
或者
LPCSTR result=(to_string(OurVariable).c_str());
it really works
真的行
回答by user9559196
Use _T()
decorator for Unicode-aware code:
_T()
对 Unicode 感知代码使用装饰器:
int number = 1;
CString t;
t.Format(_T("%d"), number);
AfxMessageBox(t);
参考:https: //social.msdn.microsoft.com/Forums/vstudio/en-US/f202b3df-5849-4d59-b0d9-a4fa69046223/how-to-convert-int-to-lpctstr?forum =vclanguage