wcstoul

Converts the initial portion of the wide-character string pointed to by nptr to an unsigned long integer.

Format

#include  <wchar.h>

unsigned long int wcstoul  (const wchar_t *nptr, wchar_t
                           **endptr, int base);
Function Variants This function also has variants named _wcstoul32 and _wcstoul64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

Arguments

nptr
A pointer to the wide-character string to be converted to an unsigned long.
endptr
The address of an object where the function can store the address of the first unrecognized character encountered in the conversion process (The character that follows the last character in the string being converted). If endptr is a NULL pointer, the address of the first unrecognized character is not retained.
base
The value, 2 through 36, to use as the base for the conversion.

If base is 16, leading zeros after the optional sign are ignored, and 0x or 0X is ignored.

If base is 0, the sequence of characters is interpreted by the same rules used to interpret an integer constant: after the optional sign, a leading 0 indicates octal conversion, a leading 0x or 0X indicates hexadecimal conversion, and any other combination of leading characters indicates decimal conversion.

Description

This function recognizes strings in various formats, depending on the value of the base. It ignores any leading white-space characters (as defined by the iswspace function) in the string. It recognizes an optional plus or minus sign, then a sequence of digits or letters that may represent an integer constant according to the value of the base. The first unrecognized wide character ends the conversion.

Return Values
The converted value. 
Indicates that the string starts with an unrecognized wide character or that the value for base is invalid. If the string starts with an unrecognized wide character, *endptr is set to nptr. The function sets errno to EINVAL. 
ULONG_ MAX  Indicates that the converted value would cause an overflow. The function sets errno to ERANGE. 

Example

    #include <stdlib.h>
    #include <stdio.h>
    #include <wchar.h>
    #include <errno.h>
    #include <limits.h>
    
    /* This test calls wcstoul() to convert a string to an unsigned long */
    /* integer. The resulting integer and any characters that could not  */
    /* be converted are output.                                          */
    
    #define MAX_STRING 128
    
    main()
    {
    
    int  base=10, errno;
    char *input_string="1234.56";
    wchar_t string_array[MAX_STRING], *ptr;
    size_t size;
    unsigned long int val;
    
    printf("base = [%d]\n",base);
    printf("String to convert = %s\n",input_string);
    if ((size = mbstowcs(string_array,input_string,MAX_STRING)) == (size_t)-1)
            {
            perror("mbstowcs");
            exit(EXIT_FAILURE);
            }
    printf("wchar_t string is = [%S]\n",string_array);
    
    errno = 0;
    val = wcstoul(string_array, &ptr, base);
    if (errno == 0)
            {
            printf("returned unsigned long int from wcstoul = [%u]\n",val);
            printf("wide char terminating scan(ptr) = [%S]\n\n",ptr);
            }
    if (errno == ERANGE)
            {
            perror("error value is :");
            printf("ULONG_MAX = [%u]\n",ULONG_MAX);
            printf("wcstoul failed, val = [%d]\n\n",val);
            }
    
    }
    

Running the example program produces the following result:

base = [10]
String to convert = 1234.56
wchar_t string is = [1234.56]
returned unsigned long int from wcstoul = [1234]
wide char terminating scan(ptr) = [.56]


Previous Page | Next Page | Table of Contents | Index