1

I want to get int from cstring. Here is Code.


CStringArray paramArray;
paramArray.Add((LPCSTR)"5");
paramArray.Add((LPCTSTR)"151");

pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];

CString str;
str = (CStringA)pvarArguments[0];
int nlen = _wtoi(str.GetBuffer());

When I run my program, I always get value 0, and I can't understand why it is. Please help me.

bTagTiger
  • 1,261
  • 5
  • 23
  • 38
  • 4
    Are you sure that `paramArray[1]` is holding your `CString`? From the snipped you posted, I would expect it to be `paramArray[0]`. – Chad Jul 30 '12 at 19:14
  • 1
    Do you have `UNICODE` defined? If not the `LPCTSTR` is just a `char*` and `_wtoi` would not be an appropriate function to call – YePhIcK Jul 30 '12 at 19:16
  • Oh , I'm very sorry for about my mistake. The Codde must be like this. paramArray.Add((LPCSTR)"5");; paramArray.add(((LPCSTR)"151"); and the ohher code is the same – bTagTiger Jul 31 '12 at 02:39
  • @bTagTiger Please edit your question with this new information rather than a comment. – acraig5075 Jul 31 '12 at 04:07

5 Answers5

1

From TFM (emphasis mine):

Each function returns the int value produced by interpreting the input characters as a number. The return value is 0 for atoi and _wtoi, if the input cannot be converted to a value of that type.

Print the string or examine it using a debugger. There may be invalid (including unprintable) characters in the string.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • I would suggest to print or view the byte values of each character of the string, rather than relying on the debugger or printing, since as you say in certain circumstances the invalid characters may not show up. – NominSim Jul 30 '12 at 19:15
0

CStringA implies ANSI string type which would require atoi not _wtoi.

I suggest:

CString str(pvarArguments[0]);
int nlen = atoi(str.GetString());

GetBuffer() is not ideal because you must remember to later ReleaseBuffer().

EDIT: In light of the new information, try this:

paramArray.Add(L"5");
paramArray.Add(L"151");

The L macro makes the string wchar_t aware. If L doesn't work try _T instead. And then use _wtoi or _tstoi.

Community
  • 1
  • 1
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • When I insert this code to my project , It occurs error that "Can not convert parameter1 from const wchar_t * to const char*. So I changed functions name to _wtoi, not atoi. But the error is the same. Value is 0. – bTagTiger Jul 31 '12 at 02:46
  • @bTagTiger Did you see my edit to my answer, and did the `L` macro work for you? – acraig5075 Aug 01 '12 at 06:17
0

It's hard to tell even what you are trying to do. You do know that C++ arrays are 0-based, right? I ask because this line of code:

pvarArguments[0] = (LPCTSTR)paramArray[1];

is totally messed up. I don't understand why it's not throwing an exception when trying to index an element in a CStringArray that is equal to the count of elements. You can only index to count-1 ==>> which in this case is "0".

Your pvarArguments[0] will have junk in it--I have no idea why an exception wasn't thrown.

If you want to get a different type out of a variant, you can use VariantChangeType() and not mess with wcstoi or atoi. It will give an error code if it fails instead of just returning 0. If you are hell bent on using wcstoi or atoi on a CString, use _tstoi() which works whether you nave UNICODE defined or not.

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29
  • It's C++. Go out of bounds indexing an array, and demons may fly out your nose… or you'll crash… or you'll just get random garbage that happened to be past the array in memory. _Edit:_ Oops, it's an MFC collection. I have no idea if those are supposed to throw exceptions when you do something silly. – Left For Archive Jul 30 '12 at 22:26
0
CStringArray paramArray;
paramArray.Add(_T("5"));
paramArray.Add(_T("151"));

CComVariant *pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];

CString str;
str = pvarArguments[0].bstrVal;
int nlen = _ttoi(LPCTSTR(str));
mfc
  • 1
0

per suggestion above, this compiles for me:

rs->GetFieldValueString(0).Left(1) == sBoroCode
    && (_ttoi(LPCTSTR(sLowHouseNo)) % 2) == (_ttoi(LPCTSTR(rs->GetFieldValueString(2))) % 2)
Himanshu
  • 4,327
  • 16
  • 31
  • 39
user3784130
  • 71
  • 1
  • 2