strtoulのヘルプ・マニュアル
日本語 英語
strtoul --help
man strtoul
STRTOUL(3) Linux Programmer’s Manual STRTOUL(3)
名前
strtoul, strtoull, strtouq - 文字列を unsigned long int に変換する
書式
#include
unsigned long int strtoul(const char *nptr, char **endptr, int base);
unsigned long long int strtoull(const char *nptr, char **endptr,
int base);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
strtoull(): XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE ||
_ISOC99_SOURCE; or cc -std=c99
説明
strtoul() 関数は、 nptr の文字列の最初の部分を、 base を 基 数 と し て
unsigned long int に変換する。この base は 2 から 36 までの値あるいは特
別な意味を持つ値 0 でなければならない。
文字列の先頭には、任意の数の空白があってもよく (空白は (isspace(3) で判
定 さ れる) 、また数字の直前には '+' か '-' の符号があってもよい。 base
が 0 または 16 の場合には、 文字列の先頭に "0x" を置くことができ、そ の
場 合には文字列は 16進数として扱われる。これ以外の文字列で base が 0 の
場合は、文字列が '0' で始まるときは 8進数として、それ以外のときは 10 進
数として扱われる。
文字列の残りの部分は unsigned long int に変換されるが、基数に対して有効
でない数字が現れた時点で変換は終了する。(11進数以上では 'A' は大文字 ・
小文字に関わらず 10 を表し、 'B' は 11 を表現し, 以下同様に、 'Z' は 35
を表す。)
endptr がヌル値 (NULL) でない場合、 strtoul() は最初に不正な文字が現 れ
たアドレスを *endptr に格納している。文字列に有効な数字がひとつもなけれ
ば、 strtoul() は nptr の元の値を *endptr に代入する(そして 0 を返す)。
特に、 *nptr が '\0' 以外で、返された **endptr が '\0' ならば、文字列全
体が有効だったことになる。
strtoull() 関数は strtoul() 関数と同様だが、 unsigned long long int 型
の値を返す。
返り値
strtoul() 関数は変換結果を返す。文字列がマイナス符号から始まっていた場
合は、(符号反転前の値がオーバーフローしていなければ) 変換結果を符号反転
し た 値 を unsigned 型 で返す。変換でオーバーフローが発生した場合は
ULONG_MAX を返し、 errno に ERANGE を 設 定 す る 。 (ULONG_MAX を
ULLONG_MAX と読み替えれば) strtoull() は strtoul() と全く同じである。
エラー
EINVAL (C99 にはない) 与えられた base がサポートされていない値である。
ERANGE 文字列が数値の表現範囲外である。
実 装によっては、全く変換が行われなかった場合(全く数字が現れず、 0 が返
される)、errno に EINVAL がセットされるかもしれない。
準拠
strtoul() は SVr4, C89, C99 と POSIX-2001 に準拠している 。 strtoull()
は C99 と POSIX.1-2001 に準拠している。
注意
strtoul () からは成功、失敗どちらの場合でも 0 や ULONG_MAX (strtoull()
では ULLONG_MAX) が返る可能性があるので、プログラムは関数を呼び出す前に
errno を 0 に設定し、呼び出し後に errno が 0 以外の値かどうかを確認しエ
ラーが発生したかどうかを判断する必要がある。
"C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない 。
(例えば、現在のロケールの 1000 毎の区切り文字に対応しているかもしれない
。)
BSD には、
u_quad_t strtouq(const char *nptr, char **endptr, int base);
という完全に同様の定義を持つ関数がある。使用中のアーキテクチャのワー ド
長 次第であるが、この関数は strtoull() や stroul() と等価な場合もありえ
る。
負の値も正当な入力とみなされ、エラーもなく、対応する unsigned long int
型の値に変換される。
例
strtol(3) のマニュアルページの例を参照。このページで説明した関数の使用
方法も同様である。
関連項目
atof(3), atoi(3), atol(3), strtod(3), strtol(3)
GNU 2007-07-26 STRTOUL(3)
STRTOUL(3) Linux Programmer’s Manual STRTOUL(3)
NAME
strtoul, strtoull, strtouq - convert a string to an unsigned long inte-
ger
SYNOPSIS
#include
unsigned long int strtoul(const char *nptr, char **endptr, int base);
unsigned long long int strtoull(const char *nptr, char **endptr,
int base);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strtoull(): XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE ||
_ISOC99_SOURCE; or cc -std=c99
DESCRIPTION
The strtoul() function converts the initial part of the string in nptr
to an unsigned long int value according to the given base, which must
be between 2 and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as deter-
mined by isspace(3)) followed by a single optional '+' or '-' sign. If
base is zero or 16, the string may then include a "0x" prefix, and the
number will be read in base 16; otherwise, a zero base is taken as 10
(decimal) unless the next character is '0', in which case it is taken
as 8 (octal).
The remainder of the string is converted to an unsigned long int value
in the obvious manner, stopping at the first character which is not a
valid digit in the given base. (In bases above 10, the letter 'A' in
either upper or lower case represents 10, 'B' represents 11, and so
forth, with 'Z' representing 35.)
If endptr is not NULL, strtoul() stores the address of the first
invalid character in *endptr. If there were no digits at all, str-
toul() stores the original value of nptr in *endptr (and returns 0).
In particular, if *nptr is not '\0' but **endptr is '\0' on return, the
entire string is valid.
The strtoull() function works just like the strtoul() function but
returns an unsigned long long int value.
RETURN VALUE
The strtoul() function returns either the result of the conversion or,
if there was a leading minus sign, the negation of the result of the
conversion represented as an unsigned value, unless the original (non-
negated) value would overflow; in the latter case, strtoul() returns
ULONG_MAX and sets errno to ERANGE. Precisely the same holds for str-
toull() (with ULLONG_MAX instead of ULONG_MAX).
ERRORS
EINVAL (not in C99) The given base contains an unsupported value.
ERANGE The resulting value was out of range.
The implementation may also set errno to EINVAL in case no conversion
was performed (no digits seen, and 0 returned).
CONFORMING TO
strtoul() conforms to SVr4, C89, C99 and POSIX-2001, and strtoull() to
C99 and POSIX.1-2001.
NOTES
Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for
strtoull()) on both success and failure, the calling program should set
errno to 0 before the call, and then determine if an error occurred by
checking whether errno has a non-zero value after the call.
In locales other than the "C" locale, other strings may be accepted.
(For example, the thousands separator of the current locale may be sup-
ported.)
BSD also has
u_quad_t strtouq(const char *nptr, char **endptr, int base);
with completely analogous definition. Depending on the wordsize of the
current architecture, this may be equivalent to strtoull() or to str-
toul().
Negative values are considered valid input and are silently converted
to the equivalent unsigned long int value.
EXAMPLE
See the example on the strtol(3) manual page; the use of the functions
described in this manual page is similar.
SEE ALSO
atof(3), atoi(3), atol(3), strtod(3), strtol(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2007-07-26 STRTOUL(3)