Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The msdn article says:</p> <blockquote> <p>"The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The function stops reading the input string at the first character that it cannot recognize as part of a number."</p> </blockquote> <p>If you test the code with unicode strings containing actual numbers, you'll see the correct output:</p> <pre><code>printf("TEST 1: %d \n", _tstoi(_T("1"))); </code></pre> <p>output:</p> <pre><code>TEST 1: 1 </code></pre> <p>Like @Ylisar said, the *toi functions are used to convert number values from strings to integer variables instead.</p> <p>The following code will output the number representation instead, but watch out for the pointer representation of the const variables. I've left both versions so you can see the difference:</p> <pre><code> printf("TEST 1: %d \n", _tstoi(_T("1"))); printf("TEST a: %d \n", _tstoi(_T("a"))); WCHAR* b(_T("b")); printf("TEST A: %d \n", _T("A")); printf("TEST b: %d \n", *b); </code></pre> <p>Output:</p> <pre><code>TEST 1: 1 TEST a: 0 TEST A: 13457492 TEST b: 98 </code></pre> <p>Check out more at <a href="http://msdn.microsoft.com/en-us/library/yd5xkb5c%28v=vs.80%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/yd5xkb5c%28v=vs.80%29.aspx</a></p> <p>If you want to sum up (accumulate) the values, I would recommend you checking out the STL range functions which does wonders on such things. For example</p> <pre><code>#include &lt;numeric&gt; #include &lt;string&gt; printf("TEST a: %d \n", *_T("a")); // 97 printf("TEST b: %d \n", *_T("b")); // 98 wstring uString(_T("ba")); int result = accumulate(uString.begin(), uString.end(), 0); printf("TEST accumulated: %d \n", result); </code></pre> <p>Results:</p> <pre><code>TEST a: 97 TEST b: 98 TEST accumulated: 195 </code></pre> <p>This way you don't have to have for-loops going through all the values. The range functions really are nice for stuff like this.</p> <p>Check out more at: <a href="http://www.sgi.com/tech/stl/accumulate.html" rel="nofollow">http://www.sgi.com/tech/stl/accumulate.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload