CString to Integer / Double

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

CString to Integer / Double

 
0
  #1
Nov 30th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,359
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: CString to Integer / Double

 
0
  #2
Nov 30th, 2008
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
Last edited by Ancient Dragon; Nov 30th, 2008 at 1:29 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 35
Reputation: koushal.vv is an unknown quantity at this point 
Solved Threads: 1
koushal.vv koushal.vv is offline Offline
Light Poster

Re: CString to Integer / Double

 
0
  #3
Nov 30th, 2008
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

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

Resetting would give any side effects ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC