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

  1. /*****************************************************************************/
  2. /***     Include files                                                     ***/
  3. /*****************************************************************************/
  4.  
  5. #include <stdlib.h>                     /* Standard libarary.                */
  6. #include <stdio.h>                      /* Standard IO functions.            */
  7.  
  8. #include <wchar.h>                      /* XPG/4 library.                    */
  9. #include <locale.h>                     /* Locale definitions.               */
  10.  
  11. /*****************************************************************************/
  12. /***     Do some wchar_t conversions                                       ***/
  13. /*****************************************************************************/
  14.  
  15. void do_w_conversions()
  16. {
  17.   char    a_char, b_char;               /* Characters used in conversion.    */
  18.   wchar_t a_w_char;                     /* Wide character used in conversion.*/
  19.   char    a_string[100], b_string[100]; /* Strings used in conversions.      */
  20.   wchar_t a_w_string[50];               /* String of wchar_t for conversion. */
  21.  
  22.                                         /* Assign a string into a variable.  */
  23.                                         /* Convert it to a wide string.      */
  24.                                         /* Convert the wide string back to a */
  25.                                         /*  multibyte string.                */
  26.                                         /* Print out all three strings.      */
  27.   strcpy(a_string, "Hello there!");
  28.   mbstowcs(a_w_string, a_string, 50);
  29.   wcstombs(b_string, a_w_string, 100);
  30.   printf("Original string:  %s\nConverted string: %S\nConverted back:   %s\n",
  31.          a_string, a_w_string, b_string);
  32.  
  33.                                         /* Assign a char into a variable.    */
  34.                                         /* Convert it to a wide char.        */
  35.                                         /* Convert the wide char back to a   */
  36.                                         /*  multibyte char.                  */
  37.                                         /* Print out all three chars.        */
  38.   a_char = 'K';
  39.   mbtowc(&a_w_char, &a_char, 2);
  40.   wctomb(&b_char, a_w_char);
  41.   printf("\nOriginal char:  %c\nConverted char: %C\nConverted back: %c\n",
  42.          a_char, a_w_char, b_char);
  43.  
  44.                                         /* Print out the length (in bytes)   */
  45.                                         /*  of a multibyte character.        */
  46.                                         /* Print out the width (in chars) of */
  47.                                         /*  a wide character.                */
  48.                                         /* Print out the width (in chars) of */
  49.                                         /*  a wide string.                   */
  50.   printf("\nLength of multibyte character: '%c' is: %d\n", a_char,
  51.          mblen(&a_char, MB_CUR_MAX));
  52.   printf("\nWidth of wchar_t character: '%C' is: %d\n", a_w_char,
  53.          wcwidth(a_w_char));
  54.   printf("\nWidth of wchar_t string: '%S' is: %d\n", a_w_string,
  55.          wcswidth(a_w_string, wcslen(a_w_string)));
  56. }
  57.  
  58. /*****************************************************************************/
  59. /***     Main program                                                      ***/
  60. /*****************************************************************************/
  61.  
  62. void main(void)
  63. {
  64.                                         /* Set the locale based on env var.  */
  65.                                         /* Print out the locale.             */
  66.   setlocale(LC_ALL, "");
  67.   printf("\nCurrent locale is: %s\n\n", setlocale(LC_CTYPE, NULL));
  68.  
  69.                                         /* Do some wchar_t conversions.      */
  70.   do_w_conversions();
  71. }
  72.