将char数组(c字符串)转换为c中的_int64

时间:2021-12-22 12:10:13

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

我尝试用这个函数将字符串转换为_int64,但没有成功:

_int64 lKey = _atoi64("a1234");

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

lKey值总是为0并且不能工作,除了字符串是像“1234”这样的数字

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

我使用c++字符串流读取解决方案,但我想用纯C编写应用程序

2 个解决方案

#1


3  

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.

每个函数返回通过将输入字符解释为数字而产生的__int64值。如果输入不能转换为该类型的值,则_atoi64的返回值为0。

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.

所以必须确保传递了正确的字符串。否则,返回值总是为零。就这个函数而言,“a1234”不是一个正确的字符串,而且几乎每个“dump”解析函数都无法解析它。

#2


2  

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

如果你认为你的数字是十六进制,而C99是好的,你可能想试试strtoull():

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

Or with auto-detect:

或与自动检测:

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

#1


3  

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.

每个函数返回通过将输入字符解释为数字而产生的__int64值。如果输入不能转换为该类型的值,则_atoi64的返回值为0。

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.

所以必须确保传递了正确的字符串。否则,返回值总是为零。就这个函数而言,“a1234”不是一个正确的字符串,而且几乎每个“dump”解析函数都无法解析它。

#2


2  

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

如果你认为你的数字是十六进制,而C99是好的,你可能想试试strtoull():

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

Or with auto-detect:

或与自动检测:

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