名前空間
変種
操作

strtol, strtoll

提供: cppreference.com
< c‎ | string‎ | byte
ヘッダ <stdlib.h> で定義
long      strtol( const char          *str, char          **str_end, int base );
(C99未満)
long      strtol( const char *restrict str, char **restrict str_end, int base );
(C99以上)
long long strtoll( const char *restrict str, char **restrict str_end, int base );
(C99以上)

str の指すバイト文字列内の整数値を解釈します。

最初の非ホワイトスペース文字が見つかるまで、あらゆるホワイトスペース文字を破棄します (ホワイトスペースは isspace() を呼ぶことによって識別されます)。 その後、有効な base 進数の整数表現を形成する文字を可能な限り多く読み込み、それを整数値に変換します。 有効な整数値は以下のように構成されます。

  • (オプション) 正または負の符号
  • (オプション) 8進数を表す接頭辞 0 (基数が 8 または 0 の場合のみ)
  • (オプション) 16進数を表す接頭辞 0x または 0X (基数が 16 または 0 の場合のみ)
  • 数字の並び

基数に対する有効な値の集合は {0,2,3,...,36} です。 2進数の整数に対する有効な数字の集合は {0,1} で、3進数に対しては {0,1,2} で、以下同様です。 10 より大きな基数に対しては、有効な数字にアルファベット文字が含まれます。11進数用の Aa から始まり、36進数用の Zz までです。 大文字小文字の違いは無視されます。

現在設定されている C のロケールによって、追加の数値形式が受理されるかもしれません。

base の値が 0 の場合、基数は自動検出されます。 接頭辞が 0 であれば8進数、 0x または 0X であれば16進数、そうでなければ10進数です。

入力列の一部に負の符号があった場合、結果の型で単項マイナス演算子によって行われたかのように、その数字列から計算された数値の符号が反転されます。

この関数は str_end の指すポインタを、解釈した最後の文字の次の文字を指すように設定します。 str_endNULL の場合は無視されます。

str が空または期待される形式を持たない場合、変換は行われず、 (str_endNULL でなければ) str の値が str_end の指すオブジェクトに格納されます。

目次

[編集] 引数

str - 解釈されるヌル終端バイト文字列を指すポインタ
str_end - 文字へのポインタへのポインタ
base - 解釈される整数値の基数

[編集] 戻り値

  • 成功した場合は、 str の内容に対応する整数値が返されます。
  • 変換後の値が戻り値の型に対応する範囲外の場合は、値域エラーが発生し (errnoERANGE に設定し)、 LONG_MAXLONG_MINLLONG_MAX または LLONG_MIN が返されます。
  • 変換が行えない場合は、 0 が返されます。

[編集]

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
 
int main(void)
{
    // parsing with error handling
    const char *p = "10 200000000000000000000000000000 30 -40 junk";
    printf("Parsing '%s':\n", p);
    char *end;
    for (long i = strtol(p, &end, 10);
         p != end;
         i = strtol(p, &end, 10))
    {
        printf("'%.*s' -> ", (int)(end-p), p);
        p = end;
        if (errno == ERANGE){
            printf("range error, got ");
            errno = 0;
        }
        printf("%ld\n", i);
    }
 
    // parsing without error handling
    printf("\"1010\" in binary  --> %ld\n", strtol("1010",NULL,2));
    printf("\"12\" in octal     --> %ld\n", strtol("12",NULL,8));
    printf("\"A\"  in hex       --> %ld\n", strtol("A",NULL,16));
    printf("\"junk\" in base-36 --> %ld\n", strtol("junk",NULL,36));
    printf("\"012\" in auto-detected base  --> %ld\n", strtol("012",NULL,0));
    printf("\"0xA\" in auto-detected base  --> %ld\n", strtol("0xA",NULL,0));
    printf("\"junk\" in auto-detected base -->  %ld\n", strtol("junk",NULL,0));
}

出力:

Parsing '10 200000000000000000000000000000 30 -40 junk':
'10' -> 10
' 200000000000000000000000000000' -> range error, got 9223372036854775807
' 30' -> 30
' -40' -> -40
"1010" in binary  --> 10
"12" in octal     --> 10
"A"  in hex       --> 10
"junk" in base-36 --> 926192
"012" in auto-detected base  --> 10
"0xA" in auto-detected base  --> 10
"junk" in auto-detected base -->  0

[編集] 参考文献

  • C11 standard (ISO/IEC 9899:2011):
  • 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 344-345)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 310-311)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.10.1.5 The strtol function

[編集] 関連項目

バイト文字列を整数値に変換します
(関数) [edit]
バイト文字列を符号無し整数値に変換します
(関数) [edit]
(C95)(C99)
ワイド文字列を整数値に変換します
(関数) [edit]
ワイド文字列を符号無し整数値に変換します
(関数) [edit]
strtol, strtollC++リファレンス