tyrion

chars.c

Jan 18th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <locale.h>
  5. // #include <wchar.h>
  6. // #include <wctype.h>
  7.  
  8. // funzione che ritorna un array di byte corrispondenti al valore dell'intero
  9. // passato come argomento (dato che un intero può essere di più byte ...)
  10. // si potrebbe mettere anche long x...
  11. char* inttobyt(int x) {
  12.     int length = ceilf(log2f(x)/8);
  13.     char* bytes = malloc(length);
  14.  
  15.     for (int i = length-1; i >= 0; i--) {
  16.         bytes[i] = x % 256;
  17.         x /= 256;
  18.     }
  19.     return bytes;
  20. }
  21.  
  22. void printchars(void *bytes, int size) {
  23.     // printf("%d\n", sizeof *bytes);
  24.     unsigned char *cp = bytes;
  25.     for (int i = 0; i < size; i++)
  26.         putchar(cp[i]);
  27.     putchar('\n');
  28. }
  29.  
  30.  
  31. void printbytes(void *bytes, int size) {
  32.     unsigned char *cp = bytes;
  33.     printf("%p: ", cp);
  34.     for (int i = 0; i < size; ++i)
  35.         printf("%02X ", *(cp + i));
  36.     putchar('\n');
  37. }
  38.  
  39. int main() {
  40.     // wint_t wc;
  41.  
  42.     if ( setlocale( LC_CTYPE, "" ) == NULL)
  43.     {
  44.       fprintf( stderr,
  45.                 "Sorry, couldn't change to the system's native locale.\n");
  46.       return 1;
  47.     }
  48.  
  49.  
  50.     // wchar_t lol = L'\u00E4';
  51.     // wchar_t asd = L'ä';
  52.     // putwchar(L'\u00E4');
  53.  
  54.     // putwchar((wchar_t)228);
  55.     // putchar(0xC3);
  56.     // putchar(0xA4);
  57.     // putchar(0xC3A4);
  58.     // printf("%c => MB: %X\tWC: %X\n", 'ä', 'ä', L'ä');
  59.  
  60.  
  61.     // int a = 'ä';
  62.     wchar_t b = L'ä';
  63.     char a[2] = { };
  64.     wctomb(a, b);
  65.     // stampa il carattere ä (se si utilizza utf8)
  66.     // printchars(inttobyt(0xC3A4), 2);
  67.     // printchars(a, 2);
  68.  
  69.     unsigned char c[2] = {97,98};
  70.     printchars(&c, 2);
  71.  
  72.     int x = 0xC3A4;
  73.     printbytes(&x, sizeof x);
  74.     printbytes(&a, sizeof a);
  75.     printbytes(&c, 2);
  76.    
  77.  
  78.     char d[] = "\u2603"; // snowman
  79.     char e[] = "☃";
  80.     printbytes(&d, sizeof d);
  81.     printbytes(&e, sizeof e);
  82.  
  83.     /*
  84.     while ( (wc = getwchar( )) != WEOF ) // or:  (wc = getwc( stdin))
  85.     {
  86.       // if (wc != 0xA) printf("%d\n", wc);
  87.       wc = towupper(wc);
  88.       putwchar((wchar_t)wc);        // or:  putwc( (wchar_t)wc, stdout);
  89.     }*/
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment