Question

I tried this function to convert a string to _int64 but it didn't work :

_int64 lKey = _atoi64("a1234");

lKey value is always zero and doesn't work except the string is only digits like "1234"

I read solutions using C++ string stream but I want to write my application in pure C

Était-ce utile?

La solution

The function does indeed work. As the documentation states:

Each function returns the __int64 value produced by interpreting the input characters as a number. The return value is 0 for _atoi64 if the input cannot be converted to a value of that type.

So you have to make sure that a correct string is passed. Otherwise, the return value will always be zero. "a1234" is not a correct string in terms of this function and pretty every "dump" parsing function will fail to parse it.

Autres conseils

If you consider your number to be hexadecimal, and C99 is okay, you might want to try strtoull() instead:

const unsigned long long value = strtoull(string, NULL, 16);

Or with auto-detect:

const unsigned long long value = strtoull(string, NULL, 0);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top