Examples
The following example uses the wcstod subroutine to convert a wide character string to a double-precision floating point:
#include <stdlib.h>
#include <locale.h>
#include <errno.h>
extern int errno;
main()
{
wchar_t *pwcs, *endptr;
double retval;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs point to a wide character null terminated
** string containing a floating point value.
*/
errno = 0; /* set errno to zero */
retval = wcstod(pwcs, &endptr);
if(errno != 0){
/* errno has changed, so error has occurred */
if(errno == ERANGE){
/* correct value is outside range of
** representable values. Case of overflow
** error
*/
if((retval == HUGE_VAL) ||
(retval == -HUGE_VAL)){
/* Error case. Handle accordingly. */
}else if(retval == 0){
/* correct value causes underflow */
/* Handle appropriately */
}
}
}
/* retval contains the double. */
}
The following example uses the wcstol subroutine to convert a wide character string to a signed long integer:
#include <stdlib.h>
#include <locale.h>
#include <errno.h>
#include <stdio.h>
extern int errno;
main()
{
wchar_t *pwcs, *endptr;
long int retval;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs point to a wide character null terminated
** string containing a signed long integer value.
*/
errno = 0; /* set errno to zero */
retval = wcstol(pwcs, &endptr, 0);
if(errno != 0){
/* errno has changed, so error has occurred */
if(errno == ERANGE){
/* correct value is outside range of
** representable values. Case of overflow
** error
*/
if((retval == LONG_MAX) || (retval == LONG_MIN)){
/* Error case. Handle accordingly. */
}else if(errno == EINVAL){
/* The value of base is not supported */
/* Handle appropriately */
}
}
}
/* retval contains the long integer. */
}
The following example uses the wcstoul subroutine to convert a wide character string to an unsigned long integer:
#include <stdlib.h>
#include <locale.h>
#include <errno.h>
extern int errno;
main()
{
wchar_t *pwcs, *endptr;
unsigned long int retval;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs point to a wide character null terminated
** string containing an unsigned long integer value.
*/
errno = 0; /* set errno to zero */
retval = wcstoul(pwcs, &endptr, 0);
if(errno != 0){
/* error has occurred */
if(retval == ULONG_MAX || errno == ERANGE){
/*
** Correct value is outside of
** representable value. Handle appropriately
*/
}else if(errno == EINVAL){
/* The value of base is not representable */
/* Handle appropriately */
}
}
/* retval contains the unsigned long integer. */
}