简体   繁体   English

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

[英]converting char array (c string) to _int64 in C

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值始终为零,除字符串仅是诸如“ 1234”之类的数字外不起作用

I read solutions using C++ string stream but I want to write my application in pure C 我使用C ++字符串流读取解决方案,但我想用纯C语言编写应用程序

The function does indeed work. 该功能确实有效。 As the documentation states : 文档所述

Each function returns the __int64 value produced by interpreting the input characters as a number. 每个函数返回通过将输入字符解释为数字而产生的__int64值。 The return value is 0 for _atoi64 if the input cannot be converted to a value of that type. 如果无法将输入转换为该类型的值,则_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"不是正确的字符串,几乎每个“转储”解析函数都将无法解析它。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM