我正在编写一个 Win32 程序,我想将 X pos 和 Y pos 文本输出到屏幕上,我想知道如何将 SHORT 转换为 TCHAR。不要使用 atoi 或 itoa 函数。
这是一个玩具程序,我想用文本输出鼠标的位置,但我不知道如何将 short 转换为 TCHAR。
也许您想将 unsigned int 转换为字符串。如果 TCHAR 被定义为 WCHAR,则可以使用 std::to_wstring:
short x = 123;
std::wstring s = std::to_wstring(x);
然后转换s.c_str()
为TCHAR*
.
您可以使用字符串流。
#include <sstream>
std::stringstream ss("");
ss << nX << " " << nY;
TextOut(GetDC(hWnd), 100, 100, reinterpret_cast<TCHAR *>(ss.str().c_str()), ss.str().size());
SHORT myVal;
TCHAR buf[32]; // 32 is big enough to contain a 16-bit int as a string
_stprintf_s(buf,ARRAYSIZE(buf),"%hd",myVal);
// now buf contains the text as a TCHAR