home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / NUMCONV / NUMCONV.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  8KB  |  188 lines

  1. /*****************************************************************************/
  2. /***     Global defines                                                    ***/
  3. /*****************************************************************************/
  4.  
  5. #define _IBMC32                         /* Need to turn this on son that the */
  6.                                         /*  proper prototype is used for the */
  7.                                         /*  wcstod function.                 */
  8.  
  9. /*****************************************************************************/
  10. /***     Include files                                                     ***/
  11. /*****************************************************************************/
  12.  
  13. #include <stdlib.h>                     /* Needed for strtod function.       */
  14.  
  15. #include <stdio.h>                      /* Standard IO functions.            */
  16.  
  17. #include <wchar.h>                      /* XPG/4 library.                    */
  18. #include <locale.h>                     /* Locale definitions.               */
  19.  
  20. /*****************************************************************************/
  21. /***     Global variables                                                  ***/
  22. /*****************************************************************************/
  23.  
  24. wchar_t *end;                           /* End wide string for conversions.  */
  25. char *str_end;                          /* End string for strtod conversions.*/
  26.  
  27. /*****************************************************************************/
  28. /***     Perform numeric conversion to long                                ***/
  29. /*****************************************************************************/
  30.  
  31. void do_wcstol(wchar_t *in_val, int base)
  32. {
  33.   long    ret_val;                      /* Long integer returned from conv   */
  34.  
  35.                                         /* Do the conversion.                */
  36.                                         /* Print results of conversion.      */
  37.   ret_val = wcstol(in_val, &end, base);
  38.   printf("Conversion of: '%S' is: '%ld' with end of: '%S'\n", in_val,
  39.          ret_val, end);
  40. }
  41.  
  42. void do_long_conv(void)
  43. {
  44.                                         /* Print a header.                   */
  45.                                         /* Do conversions.  NOTE:  The base  */
  46.                                         /*  of '0' tells the function to do  */
  47.                                         /*  conversions in decimal.  The last*/
  48.                                         /*  two conversions are done in base */
  49.                                         /*  2.                               */
  50.   printf("\nExamples of wcstol:\n");
  51.   printf("-------------------\n");
  52.   do_wcstol(L"1234.567", 0);
  53.   do_wcstol(L".1234567", 0);
  54.   do_wcstol(L" +1234567.", 0);
  55.   do_wcstol(L"0777", 0);
  56.   do_wcstol(L"-4.5e6ABC", 0);
  57.   do_wcstol(L"0X19AF", 0);
  58.   do_wcstol(L"0xffZAP", 0);
  59.   do_wcstol(L"1234.567", 2);
  60.   do_wcstol(L"10101.12", 2);
  61. }
  62.  
  63. /*****************************************************************************/
  64. /***     Perform numeric conversion to unsigned long                       ***/
  65. /*****************************************************************************/
  66.  
  67. void do_wcstoul(wchar_t *in_val, int base)
  68. {
  69.   unsigned long ret_val;                /* Long (unsigned) int returned.     */
  70.  
  71.                                         /* Do the conversion.                */
  72.                                         /* Print results of conversion.      */
  73.   ret_val = wcstoul(in_val, &end, base);
  74.   printf("Conversion of: '%S' is: '%lu' with end of: '%S'\n", in_val,
  75.          ret_val, end);
  76. }
  77.  
  78. void do_ulong_conv(void)
  79. {
  80.                                         /* Print a header.                   */
  81.                                         /* Do conversions.  NOTE:  The base  */
  82.                                         /*  of '0' tells the function to do  */
  83.                                         /*  conversions in decimal.  The last*/
  84.                                         /*  two conversions are done in base */
  85.                                         /*  2.                               */
  86.   printf("\nExamples of wcstoul:\n");
  87.   printf("--------------------\n");
  88.   do_wcstoul(L"1234.567", 0);
  89.   do_wcstoul(L".1234567", 0);
  90.   do_wcstoul(L" +1234567.", 0);
  91.   do_wcstoul(L"0777", 0);
  92.   do_wcstoul(L"-4.5e6ABC", 0);
  93.   do_wcstoul(L"0X19AF", 0);
  94.   do_wcstoul(L"0xffZAP", 0);
  95.   do_wcstoul(L"1234.567", 2);
  96.   do_wcstoul(L"10101.12", 2);
  97. }
  98.  
  99. /*****************************************************************************/
  100. /***     Perform numeric conversion to double from wide string             ***/
  101. /*****************************************************************************/
  102.  
  103. void do_wcstod(wchar_t *in_val)
  104. {
  105.   double ret_val;                       /* Double that is returned.          */
  106.  
  107.                                         /* Do the conversion.                */
  108.                                         /* Print results of conversion.      */
  109.   ret_val = wcstod(in_val, &end);
  110.   printf("Conversion of: '%S' is: '%lf' with end of: '%S'\n", in_val,
  111.          ret_val, end);
  112. }
  113.  
  114. void do_w_double_conv(void)
  115. {
  116.                                         /* Print a header.                   */
  117.                                         /* Do conversions.                   */
  118.   printf("\nExamples of wcstod:\n");
  119.   printf("------------------\n");
  120.   do_wcstod(L"123erg");
  121.   do_wcstod(L"1234.567");
  122.   do_wcstod(L".1234567");
  123.   do_wcstod(L"+1234567.");
  124.   do_wcstod(L"-1234.567");
  125.   do_wcstod(L"-.1234567");
  126.   do_wcstod(L" -1234567.");
  127.   do_wcstod(L"0777");
  128.   do_wcstod(L"-4.5e6ABC");
  129.   do_wcstod(L"1.2e-3XYZ");
  130.   do_wcstod(L"1.2ee3XYZ");
  131.   do_wcstod(L"1.2E3eee");
  132. }
  133.  
  134. /*****************************************************************************/
  135. /***     Perform numeric conversion to double from string                  ***/
  136. /*****************************************************************************/
  137.  
  138. void do_strtod(char *in_val)
  139. {
  140.   double ret_val;                       /* Double that is returned.          */
  141.  
  142.                                         /* Do the conversion.                */
  143.                                         /* Print results of conversion.      */
  144.   ret_val = strtod(in_val, &str_end);
  145.   printf("Conversion of: '%s' is: '%lf' with end of: '%s'\n", in_val,
  146.          ret_val, str_end);
  147. }
  148.  
  149. void do_double_conv(void)
  150. {
  151.                                         /* Print a header.                   */
  152.                                         /* Do conversions.                   */
  153.   printf("\nExamples of strtod:\n");
  154.   printf("------------------\n");
  155.   do_strtod("123erg");
  156.   do_strtod("1234.567");
  157.   do_strtod(".1234567");
  158.   do_strtod("+1234567.");
  159.   do_strtod("-1234.567");
  160.   do_strtod("-.1234567");
  161.   do_strtod("-1234567.");
  162.   do_strtod("0777");
  163.   do_strtod("-4.5e6ABC");
  164.   do_strtod("1.2e-3XYZ");
  165.   do_strtod("1.2ee3XYZ");
  166.   do_strtod("1.2E3eee");
  167. }
  168.  
  169. /*****************************************************************************/
  170. /***     Main program                                                      ***/
  171. /*****************************************************************************/
  172.  
  173. void main(void)
  174. {
  175.                                         /* Set the locale based on env var.  */
  176.                                         /* Print the locale values.          */
  177.   setlocale(LC_ALL, "");
  178.   printf("\nLocale settings: %s\n", setlocale(LC_NUMERIC, NULL));
  179.  
  180.                                         /* Perform numeric conversions.      */
  181.   do_long_conv();
  182.   do_ulong_conv();
  183.   do_w_double_conv();
  184.   do_double_conv();
  185.  
  186.                                         /* Free up memory.                   */
  187. }
  188.