Chapter Contents |
Previous |
Next |
strtoul |
Portability: | ISO/ANSI C conforming |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
DIAGNOSTICS | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <stdlib.h> unsigned long int strtoul(const char *str, char **end, int base);
DESCRIPTION |
strtoul
converts a character string to an
unsigned long
integer. The string is expected to contain the representation of
an unsigned integer in base
radix
. Base
radix
may contain an integer between 2 and 36;
if it is larger than 10, letters 'a' through 'z' (either case) are interpreted
as digits greater than 10. If
radix
is
16, a leading 0x may be present in the string, but it is ignored. Initial
white space characters are always ignored.
If
radix
is 0, the
base is determined by the first character of the string (after leading white
space and an optional sign). That is, if the string begins with 0x or 0X,
the base is assumed to be 16; if it begins with 0, it is assumed to be 8;
otherwise, it is assumed to be 10.
If the
end
value
is not 0,
*end
is modified to address the
first character of the string that is not a valid base
radix
digit. However, if no initial segment of the string can be interpreted
as an integer of appropriate base,
str
is assigned to
*end
.
RETURN VALUE |
strtoul
returns the unsigned integer value represented by the character string up
to the first unrecognized character. If no initial segment of the string
can be interpreted as an integer of appropriate base,
0UL
is returned.
DIAGNOSTICS |
If the correct value is too large to be
stored in a 370
unsigned long
,
errno
is set to ERANGE, and 232-1
is returned. (This is the value of
ULONG_MAX
, defined in
<limits.h>
.)
EXAMPLE |
#include <stdlib.h> #include <ctype.h> #include <stdio.h> unsigned long hextoul(char *); static int failed; main() { char *string; char input[20]; unsigned long output; puts("Enter a hex string to convert to unsigned long int:"); string = gets(input); output = hextoul(string); if (!failed) printf("The string is converted to: %ld\n", output); } unsigned long hextoul(char * a) { unsigned long value; char *hexstr; /* hexadecimal input string */ char *stopchar; /* where strtoul conversion stops */ hexstr = a; /* Skip space characters. */ while(isspace(*hexstr)) ++hexstr; /* Skip leading 0x. */ if (*hexstr == '0' && tolower(*(hexstr+1)) == 'x') hexstr += 2; /* refused signed hex value */ if (*hexstr == '+' || *hexstr == '-') { puts("Unsigned hex only please"); failed = 1; return -1UL; } /* Convert hex to long. */ value = strtoul(hexstr, &stopchar, 16); /* Determine whether string is valid. */ if (stopchar == hexstr) { printf("Invalid hex string: %s\n", hexstr); failed = 1; } /* Check for characters after digits. */ else if (*stopchar && !isspace(*stopchar)) printf("Extra characters after hex value ignored: %s\n", stopchar); return value; }
RELATED FUNCTIONS |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.