Thread: printing non-ASCII characters (in unicode)

  1. #1
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597

    printing non-ASCII characters (in unicode)

    I was trying to write a program that needed to output characters in Cyrillic, and someone (Fordy?) suggested unicode. What I intend to do with this program is to "rekey" a keyboard so that on keypress the output to the screen would be according to my own layout (ex: when 'H' is pressed the output would be Cyrillic letter 'X' and so on).

    first, I'm not sure whether I'm doing this right, but as I understand, Unicode would be in hex (0x0000), right? Here's my code:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #include <tchar.h>
    
    #define UNICODE
    #define _UNICODE
    #define EscKey 0x001B
    
    int _tmain(void){
      int a_key;       //key being interpreted to Cyrillic
    
      do{
        a_key = getch();
    
    //The characters output to the screen are based on 
    //the unicode table (see attachment).
        switch(a_key){
          case 'A': putchar(0x0410); break;
          case 'B': putchar(0x0411); break;
          case 'C': putchar(0x0426); break;
          case 'D': putchar(0x0414); break;
          case 'E': putchar(0x0415); break;
          case 'F': putchar(0x0424); break;
          case 'G': putchar(0x0413); break;
    
    //... this is done for all letters and any other keys I 
    //want reassigned according to the Unicode Standard 3.2 table.
          case 'X': putchar(0x0425); break;
          case 'Y': putchar(0x042B); break;
          case 'Z': putchar(0x0417); break;
    
    //other keys I reassign
          case '~': putchar(0x042E); break;
          case '{': putchar(0x0428); break;
          case '}': putchar(0x0429); break;
          case '+': putchar(0x042C); break;
          case '|': putchar(0x042D); break;
    
          case '`': putchar(0x044E); break;
          case '[': putchar(0x0448); break;
          case ']': putchar(0x0449); break;
          case '=': putchar(0x044C); break;
          case '\\': putchar(0x044D); break;
          
    //this takes care of backspace
          case '\b': //backspace
            putchar('\b');
            putchar(' ');
    //everything else gets printed "as is".
          default: putchar(a_key);
        }
      }while(a_key != EscKey);
    
      rewind (stdin);
      getchar();
    
      return (0);
    }
    Now, for some reason I am unable to get anything displayed this way in Cyrillic, while if I did the same for Latin characters (according to the same table) it works just fine. My guess that the problem lies not in my use of the table but in putchar();

    Is it correct to guess that putchar() simply doesn't have the capability to print out non-ASCII characters?

  2. #2
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    I'm really unsure how these #defines work at all in this case... I simply used th code Fordy gave me (at FD):
    Originally posted by Fordy
    If the chars are represented by unicode;
    Code:
    #define UNICODE
    #define _UNICODE
    #include <tchar.h>
    
    int _tmain(){
    
    return 0;
    }
    Can you explain to me the intent of putting those #defines in there?
    For instance, does
    case '`': putchar(0x044E); break;
    output 'N', which is 0x4E in ASCII?
    You're right, it outputs an 'N'.

    Also, I just tried putwchar() (with DevC++ Mingw comiler), and was only able to output ASCII characters using unicode, the other ones don't get printed at all. At least putchar() pinted something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  3. ascii characters
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 05:26 AM
  4. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM
  5. Printing characters not in the ASCII table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 01:47 PM