共用方式為


strtol、 wcstol、 _strtol_l、 _wcstol_l

將字串轉換為 long 整數值。

long strtol(
   const char *nptr,
   char **endptr,
   int base 
);
long wcstol(
   const wchar_t *nptr,
   wchar_t **endptr,
   int base 
);
long _strtol_l(
   const char *nptr,
   char **endptr,
   int base,
   _locale_t locale
);
long _wcstol_l(
   const wchar_t *nptr,
   wchar_t **endptr,
   int base,
   _locale_t locale
);

參數

  • nptr
    要轉換的 null 結尾字串。

  • endptr
    停止掃描的字元指標。

  • base
    若要使用的數目已經基底。

  • locale
    若要使用的地區設定。

傳回值

strtol傳回值表示字串中的nptr,但當表示會造成溢位,則它傳回LONG_MAX或LONG_MIN。 strtol如果可以不執行任何轉換,則傳回 0。 wcstol傳回值為 analogously strtol。 對於這兩個函式中, errno設定為 [ ERANGE發生溢位或反向溢位。

請參閱 _doserrno、 errno、 _sys_errlist,以及 _sys_nerr 如需有關這些及其他的傳回碼。

備註

The strtol function converts nptr to a long. strtol停止讀取字串nptr在它無法辨識的數字的第一個字元。 這可能是結束的 null 字元,或可能是第一個數字大於或等於base。

wcstol寬字元版本的strtol。 其nptr引數為寬字元字串。 這些函式具有相同其他方式作業。

泛用文字常式對應

TCHAR。H 常式

_UNICODE & 未定義的 _MBCS

定義的 _MBCS

定義 _unicode 之後

_tcstol

strtol

strtol

wcstol

_tcstol_l

_strtol_l

_strtol_l

_wcstol_l

目前的地區設定LC_NUMERIC 類別的設定會決定辨識基數字元的nptr*;*如需詳細資訊,請參閱 setlocale。 函式_l尾碼會使用目前的地區設定; _strtol_l與_wcstol_l都與對應的函式,而不_l尾碼不同之處在於它們使用地區設定中傳遞。 如需詳細資訊,請參閱 地區設定

如果endptr不是NULL,要停止掃描的字元指標儲存在所指位置endptr。 如果可以不執行任何轉換 (找不到沒有有效的數字或指定無效的基底) 的值nptr儲存在所指位置endptr。

strtol 預期nptr為指向字串的格式如下:

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

A whitespace可能會包含空格及 tab 鍵字元,會被忽略 ; digits有一或多個十進位數字。 不適合這種形式的第一個字元就會停止掃描。 如果base是介於 2 和 36,那麼它會使用的數字基底。 如果base為 0,起始的字元字串所指的nptr用來判斷基底。 如果第一個字元是 0,第二個字元不是 'x' X',字串會解譯為八進位的整數。 否則,它即解譯為十進位的數字。 如果第一個字元是 '0',第二個字元是 'x' X',字串會解譯成十六進位整數。 如果第一個字元是 '1' 到' 9',此字串會解譯為十進位整數。 字母 'a' 到 'z' (或 'A' 到 'Z') 被指派的值 10,透過 35。 字母其指派的值是小於base允許的。

需求

常式

所需的標頭

strtol

<stdlib.h>

wcstol

<stdlib.h> 或者 <wchar.h>

_strtol_l

<stdlib.h>

其他的相容性資訊,請參閱相容性在簡介中。

範例

請參閱範例的 strtod

.NET Framework 對等用法

System::Convert::ToInt64

請參閱

參考

資料轉換

地區設定

localeconv

setlocale _wsetlocale

數字值的函式的字串

strtod、 _strtod_l、 wcstod、 _wcstod_l

strtoul、 _strtoul_l、 wcstoul、 _wcstoul_l

atof、 _atof_l、 _wtof、 _wtof_l