utf8.c
changeset 114 6f6850407ccf
parent 77 49e0babccb23
equal deleted inserted replaced
113:397270b5d21a 114:6f6850407ccf
     5 
     5 
     6 #include <stdio.h>
     6 #include <stdio.h>
     7 #include <stdlib.h>
     7 #include <stdlib.h>
     8 #include <wchar.h>
     8 #include <wchar.h>
     9 #include <locale.h>
     9 #include <locale.h>
       
    10 #include <string.h>
    10 
    11 
    11 #ifndef u_char
    12 #ifndef u_char
    12 typedef unsigned char u_char;
    13 typedef unsigned char u_char;
    13 #endif
    14 #endif
    14 
    15 
    15 int main ()
    16 int main ()
    16 {
    17 {
    17     char dest[4];
    18     char dest[6];
    18 
    19 
    19     wchar_t *str = L"Unicode Example with german umlauts: ÄÖÜäöü߀\n";
    20     wchar_t *str = L"Unicode Example with german umlauts: ÄÖÜäöü߀\n";
    20 
    21 
    21     if (!setlocale (LC_CTYPE, "")) {
    22     if (!setlocale (LC_CTYPE, "")) {
    22         perror ("SETLOCALE");
    23         perror ("SETLOCALE");
    23         return EXIT_FAILURE;
    24         return EXIT_FAILURE;
    24     }
    25     }
    25 
    26 
    26     printf ("%ls", str);
    27     printf ("%ls", str);
    27 
    28 
    28     while (*str != '\n') {
    29     while (*str != L'\n') {
    29         dest[0] = 0;
    30         memset (&dest, 0, sizeof (dest));
    30         dest[1] = 0;
    31         if (wctomb (dest, (*str)) < 0)
    31         dest[2] = 0;
    32             perror ("wctomb");
    32         dest[3] = 0;
       
    33         if (wctomb (dest, (*str)) == -1)
       
    34             return EXIT_FAILURE;
       
    35 
    33 
    36         printf ("%lc -> [%4X] (%2X:%2X:%2X:%2X)\n",
    34         printf ("%lc -> [%4X] (%2X:%2X:%2X:%2X)\n",
    37                 *str, *str, (u_char) dest[0], (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]);
    35                 *str, *str, (u_char) dest[0], (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]);
    38         str++;
    36         str++;
    39     }
    37     }