Thread: Printing with Unicode characters

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Question Printing with Unicode characters

    I need to print out a Unicode character. I stored it in wchar_t type, and it stores it fine because when I use printf() to print it out and I try %d I get the correct number of the character. But I want to print out the actual character itself and I don't know how to do that (%?). Plus I want to know how to put the character (no assignment, I just type it in the code) in printf() itself but it isn't recognized that way. Maybe I need to use something besides printf()?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    I changed it to wprintf(), but still have a problem. This is the line of code:
    wprintf ("This is the hiragana: %c", j);
    And this is the error I'm getting:
    warning C4133: 'function' : incompatible types - from 'char [25]' to 'const wchar_t *'

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is j a wchar_t? Read the man page, because some of the conversion operators act differently.
    What you want is probably
    wprintf("This is the hiragana %lc", j);

    がんばって!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    Yes, it's a wchar_t.
    wchar_t j = L'が';
    I went ahead and did the change you suggested but I still get the same error. :-/

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Read the man page to find out how to use wprintf correctly
    http://www.icewalkers.com/Linux/ManPages/wprintf-3.html

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    Ok I did that and the warning's gone now, but in the console window it prints out a question mark.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Boot your computer in Jap mode. Or I think there is an environment variable that you would need to change in your program to set your character set to unicode. Research on unicode some more before attempting this.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    I found something called SetConsoleOutputCP but I don't have the associated header files (wincon.h and windows.h).

    BTW, I'm using Visual C++ Express 2005. It seems that windows.h was removed in this version or moved elsewhere?
    Last edited by chibisetsuna7; 06-18-2006 at 05:29 PM.

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    Installing it right now - see y'all in, let's see... at the rate this is going, ~200 hours. >_<

    Edit:
    And now I can't get SetConsoleOutputCP() to actually change the codepage (and I have it set to Lucida Console as the font), but GetConsoleCP() does work. I just can't change the codepage.

    Edit again:
    OK I was supposed to use GetConsoleOutputCP() and it did change the codepage apparently. But the character still shows up as a question mark.
    Last edited by chibisetsuna7; 06-18-2006 at 07:20 PM.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    What's wrong with this code?

    Code:
    #include <tchar.h>
    #include <wchar.h>
    #include <windows.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	char i = '\n';
    
    	wchar_t j = L'が';
    
            // I have also tried other codepages and checked registry
    	SetConsoleOutputCP(65001);
    
    	printf("\nHello SDL User!\n");
    	printf ("The codepage is %d\n", GetConsoleOutputCP());
    	wprintf (L"This is the hiragana: %lc", j);
    	
    	scanf_s ("%c", &i);
    	return 0;
    }

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You will have to use a console font which supports hiragana.

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    It's already in Lucida Console.

    Edit:
    I've tried to change the codepage to 932 both from the program and with chcp in the command line and it says it's invalid or something.
    Last edited by chibisetsuna7; 06-19-2006 at 09:51 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Lucida console is a Japanese font?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unicode 2 byte wide characters
    By davo666 in forum C Programming
    Replies: 18
    Last Post: 02-22-2009, 05:11 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM