字符串到数值函数String to Numeric Value Functions


strtod 系列中的每个函数都可将以空值终止的字符串转换为数值。Each function in the strtod family converts a null-terminated string to a numeric value. 下表中列出了可用函数。The available functions are listed in the following table.

函数Function 说明Description
strtod 将字符串转换为双精度浮点值Convert string to double-precision floating point value
strtol 将字符串转换为长整型Convert string to long integer
strtoul 将字符串转换为无符号长整型Convert string to unsigned long integer
_strtoi64 将字符串转换为 64 位 __int64 整数Convert string to 64-bit __int64 integer
_strtoui64 将字符串转换为 64 位无符号 __int64 整数Convert string to unsigned 64-bit __int64 integer

wcstodwcstolwcstoul_wcstoi64 分别是 strtodstrtolstrtoul_strtoi64 的宽字符版本。wcstod, wcstol, wcstoul, and _wcstoi64 are wide-character versions of strtod, strtol, strtoul, and _strtoi64, respectively. 每个宽字符函数的字符串参数都是一个宽字符串;每个函数的行为与其对应的单字节字符相同。The string argument to each of these wide-character functions is a wide-character string; each function behaves identically to its single-byte-character counterpart otherwise.

strtod 函数有两个参数:第一个是输入字符串,第二个是结束转换过程的字符的指针。The strtod function takes two arguments: the first is the input string, and the second a pointer to the character which ends the conversion process. strtolstrtoul_strtoi64_strtoui64 将第三个参数作为在转换过程中使用的数基。strtol, strtoul, _strtoi64 and _strtoui64 take a third argument as the number base to use in the conversion process.

输入字符串是一系列字符,可以解释为指定类型的数值。The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. 每个函数在首个无法识别为数字一部分的字符处停止读取字符串。Each function stops reading the string at the first character it cannot recognize as part of a number. 这可能是终止 null 字符。This may be the terminating null character. 对于 strtolstrtoul_strtoi64_strtoui64,该终止字符也可以是大于或等于用户提供的数基的第一个数字字符。For strtol, strtoul, _strtoi64, and _strtoui64, this terminating character can also be the first numeric character greater than or equal to the user-supplied number base.

如果用户提供的指向转换结束字符的指针在调用时未设置为 NULL,则会在该位置存储指向停止扫描的字符的指针。If the user-supplied pointer to an end-of-conversion character is not set to NULL at call time, a pointer to the character that stopped the scan will be stored there instead. 如果无法执行任何转换(未找到任何有效的数字或指定了无效的基数),则将在该位置存储字符串指针的值。If no conversion can be performed (no valid digits were found or an invalid base was specified), the value of the string pointer is stored at that address.

strtod 应为以下形式的字符串:strtod expects a string of the following form:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits][whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits]

whitespace 可能包含被忽略的空格或制表符;sign 是加号 (+) 或减号 (-);digits 是一个或多个十进制数字。A whitespace may consist of space or tab characters, which are ignored; sign is either plus (+) or minus (-); and digits are one or more decimal digits. 如果基数字符前没有任何数字,则基数字符后必须至少有一个数字。If no digits appear before the radix character, at least one must appear after the radix character. 十进制数字可以后跟一个指数,其中包含介绍性字母(dDeE)和可选的带符号整数。The decimal digits can be followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an optionally signed integer. 如果指数部分和基数字符都没有出现,则假定基数字符跟随字符串中的最后一个数字。If neither an exponent part nor a radix character appears, a radix character is assumed to follow the last digit in the string. 不符合此形式的第一个字符停止扫描。The first character that does not fit this form stops the scan.

strtolstrtoul_strtoi64_strtoui64 函数应为以下形式的字符串:The strtol, strtoul, _strtoi64, and _strtoui64 functions expect a string of the following form:

[whitespace] [{+ | -}] [0 [{ x | X }]] [digits][whitespace] [{+ | -}] [0 [{ x | X }]] [digits]

如果 base 参数在 2 和 36 之间,则将其用作数字的基数。If the base argument is between 2 and 36, then it is used as the base of the number. 如果为 0,则转换结束指针引用的初始字符将用于确定基数。If it is 0, the initial characters referenced to by the end-of-conversion pointer are used to determine the base. 如果第一个字符为 0,且第二个字符不为“x”或“X”,则将该字符串视为八进制整数;否则将其视为十进制数。If the first character is 0 and the second character is not 'x' or 'X', the string is interpreted as an octal integer; otherwise, it is interpreted as a decimal number. 如果第一个字符为“0”,且第二个字符为“x”或“X”,则将该字符串视为十六进制整数。If the first character is '0' and the second character is 'x' or 'X', the string is interpreted as a hexadecimal integer. 如果第一个字符是“1”至“9”,则将该字符串视为十进制整数。If the first character is '1' through '9', the string is interpreted as a decimal integer. 为字母“a”到“z”(或“A”到“Z”)分配了 10 到 35 的值;仅允许分配的值小于 base 的字母。The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less than base are permitted. strtoul_strtoui64 允许加号 (+) 或减号 (-) 前缀;前导减号表示返回值不起作用。strtoul and _strtoui64 allow a plus (+) or minus (-) sign prefix; a leading minus sign indicates that the return value is negated.

输出值受区域设置的 LC_NUMERIC 类别设置影响;有关详细信息,请参阅 setlocaleThe output value is affected by the setting of the LC_NUMERIC category setting of the locale; see setlocale for more information. 这些不带 _l 后缀的函数版本使用此区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead.

当这些函数返回的值会导致溢出或下溢,或不能转换时,将返回如下特殊情况值:When the value returned by these functions would cause an overflow or underflow, or when conversion is not possible, special case values are returned as shown:

函数Function 条件Condition Value returned
strtod 溢出Overflow +/- HUGE_VAL
strtod 下溢或不进行转换Underflow or no conversion 00
strtol + 溢出+ Overflow LONG_MAXLONG_MAX
strtol - 溢出- Overflow LONG_MINLONG_MIN
strtol 下溢或不进行转换Underflow or no conversion 00
_strtoi64 + 溢出+ Overflow _I64_MAX_I64_MAX
_strtoi64 - 溢出- Overflow _I64_MIN_I64_MIN
_strtoi64 不进行转换No conversion 00
_strtoui64 溢出Overflow _UI64_MAX_UI64_MAX
_strtoui64 不进行转换No conversion 00

_I64_MAXI64_MIN_UI64_MAX 在 LIMITS.H 中进行定义。_I64_MAX, I64_MIN, and _UI64_MAX are defined in LIMITS.H.

wcstodwcstolwcstoul_wcstoi64_wcstoui64 分别是 strtodstrtolstrtoul_strtoi64_strtoui64 的宽字符版本。指向每个宽字符函数的转换结束参数的指针都是一个宽字符串。wcstod, wcstol, wcstoul, _wcstoi64, and _wcstoui64 are wide-character versions of strtod, strtol, strtoul, _strtoi64, and _strtoui64, respectively; the pointer to an end-of-conversion argument to each of these wide-character functions is a wide-character string. 否则,每个宽字符函数将与其对应的单字节字符的行为相同。Otherwise, each of these wide-character functions behaves identically to its single-byte-character counterpart.

另请参阅See Also

数据转换 Data Conversion
区域设置 Locale
多字节字符序列的解释 Interpretation of Multibyte-Character Sequences
浮点支持 Floating-Point Support
atof、_atof_l、_wtof、_wtof_latof, _atof_l, _wtof, _wtof_l