Niklas Niklas - 20 days ago 12
C++ Question

How do I convert TCHAR to int

I want to convert a TCHAR to an int, but when I convert it I get the ASCII value to an int, not the number value.


#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[])

string in;
cin >> in;

Output: 53

But I want i to be 5.

How can this be done?


If you want to convert a string (containing an integer) to an int, you can use some C functions like atoi() (for char ANSI strings) or _wtoi() (for wchar_t Unicode UTF-16 strings), or others that take a locale as an additional input parameter: you can read more on this MSDN documentation.

If you want to use the TCHAR model, the corresponding functions are _tstoi() or _ttoi(), as described in the aforementioned MSDN documentation page.