strtol, wcstol, _strtol_l, _wcstol_l

將字串轉換成 long 整數值。

語法

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

參數

string
以 Null 終止的待轉換字串。

end_ptr
輸出參數,設定為指向最後一個解譯字元之後的字元。 忽略 ,如果 NULL 為 。

base
要使用的數字基底。

locale
要使用的地區設定。

傳回值

strtolwcstol_strtol_l_wcstol_l 會傳回 中 string 表示的值。 如果沒有可能的轉換,它們會傳回 0。 當標記法造成溢位時,它們會傳回 LONG_MAXLONG_MIN

errno 如果發生溢位或下溢,則會設定為 ERANGE 。 如果 為 ,則會將其設定為 EINVALNULLstring 或者,如果 base 為非零且小於 2,或大於 36。 如需 、 和其他傳回碼的詳細資訊 ERANGE ,請參閱 errno_doserrno_sys_errlist_sys_nerrEINVAL

備註

、、 和 函式會轉換成 stringlongstrtol_wcstol_l_strtol_lwcstol 它們會停止在第一個字元讀取 string ,但無法辨識為數字的一部分。 它可以是終止 Null 字元,或大於或等於 base 的第一個英數位元。

wcstol_wcstol_l 是寬字元版本的 strtol_strtol_l。 其 string 引數是寬字元字串。 這些函式的行為與 strtol_strtol_l 相同,否則為 。 地區設定的 LC_NUMERIC 類別設定會決定 中辨識基數位符(小數標記或小數點)。 string 函式 strtol 並使用 wcstol 目前的地區設定。 _strtol_l_wcstol_l 會改用傳入的地區設定。 如需詳細資訊,請參閱 [ setlocale ] 和 地區設定

當 為 NULLend_ptr ,會忽略它。 否則,停止掃描的字元指標會儲存在 所 end_ptr 指向的位置。 如果找不到有效的數位,或指定了不正確基底,則無法轉換。 string的值接著會儲存在 所 end_ptr 指向的位置。

strtol 需要 string 指向格式如下的字串︰

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

方括弧 ( [ ] ) 括住選擇性元素。 大括弧和垂直橫條 ( { | } ) 圍繞單一元素的替代專案。 whitespace 可能包含空格和定位字元,這些字元會被忽略。 alphanumerics 是十進位數或字母 'a''z' (或 'A''Z' )。 不符合此表單的第一個字元會停止掃描。 如果 base 介於 2 到 36 之間,則會將其作為數位的基底。 如果 base0 ,則會使用 所 string 指向字串的初始字元來判斷基底。 如果第一個字元是 0 ,而第二個字元不是 'x''X' ,則字串會解譯為八進位整數。 如果第一個字元是 '0' ,而第二個字元是 'x''X' ,則字串會解譯為十六進位整數。 如果第一個字元是 '1' 透過 '9' ,字串會解譯為十進位整數。 透過 (或 'A''Z' ) 的字母 'a''z' 會指派值 10 到 35。 掃描只允許小於 的 base 字母。 基底範圍外的第一個字元會停止掃描。 例如,假設 string 開頭為 "01" 。 如果 base0 ,掃描器會假設它是八進位整數。 '8''9' 字元會停止掃描。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcstol strtol strtol wcstol
_tcstol_l _strtol_l _strtol_l _wcstol_l

需求

常式 必要的標頭
strtol <stdlib.h>
wcstol <stdlib.h><wchar.h>
_strtol_l <stdlib.h>
_wcstol_l <stdlib.h><wchar.h>

_wcstol_l_strtol_l 式是 Microsoft 特定的,不是標準 C 程式庫的一部分。 如需相容性詳細資訊,請參閱相容性

範例

請參閱 的 strtod 範例。

另請參閱

資料轉換
地區設定
localeconv
setlocale, _wsetlocale
字串到數值函式
strtod, _strtod_l, wcstod, _wcstod_l
strtoll, _strtoll_l, wcstoll, _wcstoll_l
strtoul, _strtoul_l, wcstoul, _wcstoul_l
atof, _atof_l, _wtof, _wtof_l