Store numericals in TCHAR array into an INTEGER variable in VC++. (in UNICODE environment)

StackOverflow https://stackoverflow.com/questions/11265498

Вопрос

I had asked a question very much similar to this in the thread: https://stackoverflow.com/questions/11259474/store-the-numericals-in-char-array-into-an-integer-variable-in-vc

W.R.T. the above thread, my question is as follows:: I am working in UNICODE environment. So TCHAr would probably be treated as wchar.

My scenario is as follows:(C++)

In TCHAR a[10], the array a[] has elements (numbers) like '1','2','3' etc....

Say a[0] = '1'; a1 = '2'; a[2] = '3';

Now a[] is storing 3 characters '1', '2' and '3'. I want to store this into an int as 123 (An integer 123).

How to achieve this in C++ ?

Thanks in advance.

Это было полезно?

Решение

First, you have to null-terminate your string. Otherwise, how do you know where to stop? Then there's a function _ttoi() specifically for that.

a[3] = 0;
int n = _ttoi[a];

You have to understand the null termination bit. Depending on how do you fill the a with characters (digits), the logic of determining the end of the string might vary.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top