我想将 TCHAR 转换为 int,但是当我转换它时,我得到的 ASCII 值是 int,而不是数字值。
代码:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
int converttoint()
{
TCHAR tchar[2];
wcscpy_s(tchar, TEXT("5"));
int i = tchar[0];
cout << i << endl;
return i;
}
int main(int argc, _TCHAR* argv[])
{
converttoint();
string in;
cin >> in;
}
Output: 53
但我希望我是 5 岁。
如何做到这一点?
最佳答案
如果你想把一个字符串(包含一个整数)转换成一个int
,你可以使用一些C函数,比如atoi()
(对于char
ANSI 字符串)或 _wtoi()
(用于 wchar_t
Unicode UTF-16 字符串),或其他将语言环境作为附加输入参数的内容:您可以阅读更多在 this MSDN documentation .
如果要使用TCHAR模型,对应的函数是_tstoi()
或_ttoi()
,如前述MSDN文档页所述。
关于c++ - 如何将 TCHAR 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898346/