strtoul, wcstoul

strtoul, wcstoul

(来源:MSDN)

Convert strings to an unsigned long-integer value.

unsigned long strtoul( const char *nptr, char **endptr, intbase);

unsigned long wcstoul( const wchar_t *nptr, wchar_t **endptr, intbase);

RoutineRequired HeaderCompatibility
strtoul<stdlib.h>ANSI, Win 95, Win NT
wcstoul<stdlib.h> or <wchar.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

strtoul returns the converted value, if any, or ULONG_MAX on overflow.strtoul returns 0 if no conversion can be performed.wcstoul returns values analogously tostrtoul. For both functions,errno is set to ERANGE if overflow or underflow occurs.

Parameters

nptr

Null-terminated string to convert

endptr

Pointer to character that stops scan

base

Number base to use

Remarks

Each of these functions converts the input string nptr to an unsigned long.

strtoul stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character, or it may be the first numeric character greater than or equal tobase. TheLC_NUMERIC category setting of the current locale determines recognition of the radix character innptr; for more information, seesetlocale. If endptr is notNULL, a pointer to the character that stopped the scan is stored at the location pointed to byendptr. If no conversion can be performed (no valid digits were found or an invalid base was specified), the value ofnptr is stored at the location pointed to by endptr.

wcstoul is a wide-character version of strtoul; itsnptr argument is a wide-character string. Otherwise these functions behave identically.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined_MBCS Defined_UNICODE Defined
_tcstoulstrtoulstrtoulwcstoul

strtoul expects nptr to point to a string of the following form:

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

A whitespace may consist of space and tab characters, which are ignored;digits are one or more decimal digits. The first character that does not fit this form stops the scan. Ifbase is between 2 and 36, then it is used as the base of the number. If base is 0, the initial characters of the string pointed to bynptr are used to determine the base. 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. If the first character is '0' and the second character is 'x' or 'X', the string is interpreted as a hexadecimal integer. If the first character is '1' through '9', the string is interpreted as a decimal integer. The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less thanbase are permitted. strtoul allows a plus (+) or minus () sign prefix; a leading minus sign indicates that the return value is negated.

Example

/* STRTOD.C: This program uses strtod to convert a
 * string to a double-precision value; strtol to
 * convert a string to long integer values; and strtoul
 * to convert a string to unsigned long-integer values.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
  char   *string, *stopstring;
  double x;
  long   l;
  int    base;
  unsigned long ul;
  string = "3.1415926This stopped it";
  x = strtod( string, &stopstring );
   printf( "string = %s\n", string );
   printf("   strtod = %f\n", x );
   printf("   Stopped scan at: %s\n\n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s\n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)\n", ul, base );
      printf( "   Stopped scan at: %s\n", stopstring );
   }
}

Output

string = 3.1415926This stopped it
   strtod = 3.141593
   Stopped scan at: This stopped it

string = -10110134932This stopped it   strtol = -2147483647   Stopped scan at: This stopped itstring = 10110134932
   strtol = 45 (base 2)
   Stopped scan at: 34932
   strtol = 4423 (base 4)
   Stopped scan at: 4932
   strtol = 2134108 (base 8)
   Stopped scan at: 932


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值