STRTOU

Section: Misc. Reference Manual Pages (3bsd)
Page Index

BSD mandoc
 

NAME

strtou - convert a string to an uintmax_t integer  

LIBRARY

Lb libbsd  

SYNOPSIS

In inttypes.h (See libbsd(7) for include usage.) Ft uintmax_t Fo strtou Fa const char * restrict nptr Fa char ** restrict endptr Fa int base Fa uintmax_t lo Fa uintmax_t hi Fa int *rstatus Fc  

DESCRIPTION

The Fn strtou function converts the string in Fa nptr to an Ft uintmax_t value. The Fn strtou function uses internally strtoumax(3) and ensures that the result is always in the range [ Fa lo .. Fa hi ]. In adddition it always places 0 on success or a conversion status in the Fa rstatus argument, avoiding the errno gymnastics the other functions require. The Fa rstatus argument can be NULL if conversion status is to be ignored.

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional `+' or `-' sign. If Fa base is zero or 16, the string may then include a `0x' or `0X' prefix, and the number will be read in base 16; otherwise, a zero Fa 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 uintmax_t value in the obvious manner, stopping at the end of the string or at the first character that does not produce 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 Fa endptr is non-nil, Fn strtou stores the address of the first invalid character in Fa *endptr . If there were no digits at all, however, Fn strtou stores the original value of Fa nptr in Fa *endptr . (Thus, if Fa *nptr is not `\0' but Fa **endptr is `\0' on return, the entire string was valid.)  

RETURN VALUES

The Fn strtou function always returns the closest value in the range specified by the Fa lo and Fa hi arguments.

The errno value is guaranteed to be left unchanged.

Errors are stored as the conversion status in the Fa rstatus argument.  

EXAMPLES

The following example will always return a number in [1..99] range no matter what the input is, and warn if the conversion failed.
int e;
uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e);
if (e)
        warnc(e, "conversion of `%s' to a number failed, using %ju",
            buf, lval);
 

ERRORS

Bq Er ECANCELED
The string did not contain any characters that were converted.
Bq Er EINVAL
The base is not between 2 and 36 and does not contain the special value 0.
Bq Er ENOTSUP
The string contained non-numeric characters that did not get converted. In this case, Fa endptr points to the first unconverted character.
Bq Er ERANGE
The given string was out of range; the value converted has been clamped; or the range given was invalid, i.e. Fa lo > Fa hi .

 

SEE ALSO

atof(3), atoi(3), atol(3), atoll(3), strtod(3), strtoi(3bsd), strtoimax(3), strtol(3), strtoll(3), strtoul(3), strtoull(3), strtoumax(3)  

STANDARDS

The Fn strtou function is a Nx extension.  

HISTORY

The Fn strtou function first appeared in Nx 7 . Ox introduced the Fn strtonum 3bsd function for the same purpose, but the interface makes it impossible to properly differentiate illegal returns.  

BUGS

Ignores the current locale.


 

Index

NAME
LIBRARY
SYNOPSIS
DESCRIPTION
RETURN VALUES
EXAMPLES
ERRORS
SEE ALSO
STANDARDS
HISTORY
BUGS