Beispiele
Im folgenden Beispiel wird die Subroutine WCSTOD verwendet, um eine breite Zeichenfolge in einen Gleitkommawert mit doppelter Genauigkeit zu konvertieren:
#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. */
}
Im folgenden Beispiel wird die Subroutine Wcstol verwendet, um eine breite Zeichenfolge in eine lange ganze Zahl mit Vorzeichen zu konvertieren:
#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. */
}
Im folgenden Beispiel wird die Subroutine Wcstoul verwendet, um eine breite Zeichenfolge in eine lange ganze Zahl ohne Vorzeichen zu konvertieren:
#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. */
}