トップページ > キーワード検索 > strtoul()
C言語規格
C言語(その他)
プログラム
その他
strtoul()
機能 文字列をunsigned long int型の表現に変換する。
定義 #include <stdlib.h>

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

※C99規格では以下の定義となる
unsigned long int strtoul ( const char * restrict nptr, char ** restrict endptr, int base );
引数
nptr変換する文字列
endptr残りの部分の文字列を示すポインタの格納先を示すポインタ
base基数(2〜36の範囲)。0を指定した場合は整数定数(6(10進数), 0x6(16進数), 06(8進数)など)の形式となる
戻り値 変換された値を返す。
変換が不可能ならば 0 を返す。
正しい値が表現可能な値の範囲外である場合は、ULONG_MINまたはULONG_MAXを返し、errnoERANGEの値を格納する。
実装例
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    unsigned long int n;
    char *s;
    
    n = strtoul("101abc", &s, 2);   /* 2進数整数 */
    printf("%lu ... %s\n", n, s);
    
    n = strtoul("10", &s, 0);       /* 10進数整数 */
    printf("%lu ... %s\n", n, s);
    
    n = strtoul("010", &s, 0);      /* 8進数整数 */
    printf("%lu ... %s\n", n, s);
    
    n = strtoul("0x10", &s, 0);     /* 16進数整数 */
    printf("%lu ... %s\n", n, s);
    
    return 0;
}
実行結果
5 ... abc
10 ...
8 ...
16 ...





inserted by FC2 system