如何在 Win32 C++ 中将类型 "int" 转换为类型 "LPCSTR"
How to convert type "int" to type "LPCSTR" in Win32 C++
朋友们好,我如何将类型"int"转换为类型"LPCSTR"?我想将变量"int cxClient"赋予"MessageBox"函数的第二个参数"LPCSTR lpText"。以下是示例代码:
int cxClient;
cxClient = LOWORD (lParam);
MessageBox(hwnd, cxClient, "Testing", MB_OK);
但它不起作用。以下函数是"MessageBox"函数的方法签名:
MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType);
使用正确的sprintf变体将int转换为字符串
TCHAR buf[100];
_stprintf(buf, _T("%d"), cxClient);
MessageBox(hwnd, buf, "Testing", MB_OK);
您需要<tchar.h>
。
我认为_stprintf
是这里的快速答案,但如果你想像David建议的那样使用纯C++,那么
#ifdef _UNICODE
wostringstream oss;
#else
ostringstream oss;
#endif
oss<<cxClient;
MessageBox(0, oss.str().c_str(), "Testing", MB_OK);
你需要
#include <sstream>
using namespace std;
using std::to_string
std::string message = std::to_string(cxClient)
http://en.cppreference.com/w/cpp/string/basic_string/to_string
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 指针问题:从不兼容的类型"int"分配给"int *"
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 错误:数组下标的类型"int[int]"无效
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 从类型bankAccount的返回值到函数返回类型int没有可行的转换
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 运行时错误:有符号整数溢出:964632435 * 10 无法在类型 'int' 中表示
- 如何增加模板类型 int 参数?
- 尝试使用 Array 类的模板构造整数数组时,错误:为什么数组类型"int [5]"不可分配,
- 如何为底层类型 int 的枚举专门化类
- 数组下标编译的无效类型'int[int]'由于 -Wfatal 错误而终止
- 不能将 "const char*" 类型的值分配给类型 "int" 的实体
- 数组下标的无效类型INT [INT]
- 请求在"伺服控制器"中请求成员"附加",该成员属于非类类型"int"
- 数组类型 'int [8]'不可分配
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误