I have a function where calculated values can reach higher values than the range of unsigned __int64 which is indicated by MS by 18,446,744,073,709,551,615. How can I test if a number has exceeded that range? I've converted the int into char and tried testing by checking the length with strlen. However, some values with a length longer than specified: for example if(strlen(charvar)>17) mysteriously escape. So how can I effectively test?
Asked
Active
Viewed 578 times
0
-
Is this for the purpose of converting from string to number? What language? (C-like, but C or C++?) – sfjac Aug 24 '13 at 15:37
-
C++, it's for displaying values in an image with OpenCV (so I need to convert them to char with _ui64toa). I only want to display the ''correct'' ones. Displaying them is no problem but if I use the strlen function to get rid of the faulty ones it does not always work. – user1331044 Aug 24 '13 at 15:43
-
VS 2008, what is boost? Will take a look, isn't there some simple test method for this? It would suffice if I can test that the digit number exceeds 19 or 18. – user1331044 Aug 24 '13 at 15:53
1 Answers
1
If you can use a modern compiler or Boost, then lexical_cast
will do the job:
uint64_t bigint;
try {
bigint = lexical_cast<uint64_t>(str);
} catch (std::bad_lexical_cast &e) {
// do whatever you want to do when the string isn't valid;
}
// Safely use bigint
See this link for the Boost library. You can definitely get this for VS 2008.
If this is Windows only you can also look at _atoi64
and the like. See msdn. These return I64_MAX
and I64_MIN
in case of over/underflow.

sfjac
- 7,119
- 5
- 45
- 69
-
I get error C2065: 'lexical_cast' : undeclared identifier, so I'd need to install Boost I suppose. If there's no simpler solution I'll probably try to find an alternative (I could test the width of the number in the image). Thx for your help anyway! – user1331044 Aug 24 '13 at 16:04
-
You'd need to move to VS2012 (VC 11) to get these from studio. Boost will have them in the `boost::` namespace. But `_atoi64` may suffice. – sfjac Aug 24 '13 at 16:06
-
-
1I have found an error in my code, I placed the if condition after the display number command. Had to do it vice versa. That's why some numbers seemed not correctly measured in length with strlen. Now it works. Btw, your suggestion with _atoi64 worked as well (for __int64 though), so I'll mark your answer as accepted answer. Here's what all the fuss was about: http://videocrack.npage.de/pascalt.html the numbers with grey color are those that were too big and therefore ''incorrect'' – user1331044 Aug 24 '13 at 18:58