_atoi64, _atoi64_l, _wtoi64, _wtoi64_l

将字符串转换为 64 位整数。

语法

__int64 _atoi64(
   const char *str
);
__int64 _wtoi64(
   const wchar_t *str
);
__int64 _atoi64_l(
   const char *str,
   _locale_t locale
);
__int64 _wtoi64_l(
   const wchar_t *str,
   _locale_t locale
);

参数

str
要转换的字符串。

locale
要使用的区域设置。

返回值

每个函数均返回 __int64 值,此值是通过将输入字符解释为数字来生成的。 如果输入不能转换为 _atoi64 类型的值,则其返回值为 0。

如果函数溢出大正整型值,则返回 I64_MAX。 如果函数溢出了较大的负整型值,则返回 I64_MIN

在所有超出范围的情况下,将 errno 设置为 ERANGE。 如果传入的参数为 NULL,则调用无效参数句柄,如参数验证中所述。 如果允许执行继续,则这些功能将 errno 设置为 EINVAL,并返回 0。

备注

这些函数将字符串转换为 64 位整数值。

输入字符串是一系列字符,可以解释为指定类型的数值。 该函数在首个它无法识别为数字组成部分的字符处停止读取输入字符串。 此字符可能是终止字符串的 null 字符('\0' 或 L'\0')。

_atoi64str 参数具有以下形式:

[whitespace] [sign] [digits]

whitespace 由空格或制表符组成,可被忽略;sign是加号 (+) 或减号 (-);digits 是一个或多个数字。

_wtoi64_atoi64 相同,但前者将宽字符串用作参数。

这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前区域设置。 有关详细信息,请参阅 Locale

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

Tchar.h 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tstoi64 _atoi64 _atoi64 _wtoi64
_ttoi64 _atoi64 _atoi64 _wtoi64

要求

例程 必需的标头
_atoi64_atoi64_l <stdlib.h>
_wtoi64_wtoi64_l <stdlib.h> 或 <wchar.h>

示例

此程序说明如何使用 _atoi64 函数将存储为字符串的数字转换为数值。

// crt_atoi64.c
// This program shows how numbers stored as
// strings can be converted to numeric values
// using the _atoi64 functions.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
    char    *str = NULL;
    __int64 value = 0;

    // An example of the _atoi64 function
    // with leading and trailing white spaces.
    str = "  -2309 ";
    value = _atoi64( str );
    printf( "Function: _atoi64( \"%s\" ) = %d\n", str, value );

    // Another example of the _atoi64 function
    // with an arbitrary decimal point.
    str = "314127.64";
    value = _atoi64( str );
    printf( "Function: _atoi64( \"%s\" ) = %d\n", str, value );

    // Another example of the _atoi64 function
    // with an overflow condition occurring.
    str = "3336402735171707160320";
    value = _atoi64( str );
    printf( "Function: _atoi64( \"%s\" ) = %d\n", str, value );
    if (errno == ERANGE)
    {
       printf("Overflow condition occurred.\n");
    }
}
Function: _atoi64( "  -2309 " ) = -2309
Function: _atoi64( "314127.64" ) = 314127
Function: _atoi64( "3336402735171707160320" ) = -1
Overflow condition occurred.

另请参阅

数据转换
数学和浮点支持
区域设置
_ecvt
_fcvt
_gcvt
setlocale_wsetlocale
_atodbl_atodbl_l_atoldbl_atoldbl_l_atoflt_atoflt_l