Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top