Hi All,


i have a problem in converting CString to Int/Double,
function i am using is _wtoi , _wtof.....
Here if the CSting value has say "12345"
this works perfectly. But if any other value is say ( Over flow )
" 11111111111111111111111111111" which exceeds
the max size of int then it returns INT_MAX, where as i need the
exact string as int ie 11111111111111111111111111111 in the integer variable. same thing happens in double.

if there is any junk value entered then the ret value is 0,
Please help me in converting the CString to exact int value

Recommended Answers

All 2 Replies

There isn't much you can do about overflows, except just discard the bad CString value. It returns INT_MAX because its impossible to put that many digits into an int variable. I'd like to stuff my 100 inch waste into size 36 slacks too, but that's not possible either :)

Finally i found a way...
When ever any over flow occurs a global variable errno get's set to ERANGE so in the code i have made a check like this

int ConvertToInt( CString intVal )
{
       int ret = _wtoi(intVla);
       if((ret == 0) || (errno == ERANGE))
       {
               // Throw Exception
               // I dont know why i have to do this
               errno = 0;
               // If i dont reset the "errno" then the value will be set to   
                     //ERANGE even if the next string is valid
           //I dont know resetting the system variable is correct or not.
               // Functionality works for me :)
       }
}

Resetting would give any side effects ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.