Thread: Encoding charset to use card suits ASCII characters

  1. #16
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    ncurses shouldn't make a difference here, as we're not using wide characters, and it's just passing our text along to the console.

    What encoding are you saving your .c source files in? If you save them using UTF-8 as the encoding, and your terminal is also in UTF-8, then it should work alright. If either of those two aren't UTF-8, then no telling what you'll see. You could alternatively look up the Unicode numbers for the accents you need, encode them yourself to UTF-8, and output them just like the card symbols, but that's a lot of work.

    Also, if you "type" (copy/paste usually easiest) a spade into your source (the actual symbol), like: "♠", then you can just output that - but again, you'll have to save the source as UTF-8. (In KWrite, for example, it's File -> Save As, and then in the Save As box there is a choice of encodings.)

    One final note about UTF-8: strlen() on a UTF-8 string gives the string's length in bytes, but not in characters. (As some characters take more than one byte in UTF-8). It's just a slight gotcha to be aware of. (There is a function out there for getting the character length, but its name escapes me.)

    Edit: And to find a Unicode character's number, or just to see if it exists, the Code Charts are fairly helpful.

    Edit, again: Since you seem to know how to set your terminal to UTF-8, you should be able to verify that the file is in UTF-8 by simply "cat source.c" in the terminal.
    Last edited by Cactus_Hugger; 06-07-2007 at 09:00 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    Well, GNOME Terminal is set to UTF-8, please look at the following:
    Code:
    nazgulled@nazbox ~ $ cat utf8-normal.c 
    #include <stdio.h>
    
    int main(void) {
            // Card suits in black
            char clubs[]    = {0xE2, 0x99, 0xA7, 0};
            char diamonds[] = {0xE2, 0x99, 0xA2, 0};
            char hearts[]   = {0xE2, 0x99, 0xA1, 0};
            char spades[]   = {0xE2, 0x99, 0xA4, 0};
    
            printf("\n%s\n%s\n%s\n%s\n", clubs, diamonds, hearts, spades);
            printf("\náàâãéèêíìîĩóòôõúùûũç\n\n");
            printf("\nÁÀÂÃÉÈÊÍÌÎĨÓÒÔÕÚÙÛŨÇ\n\n");
    
            return 0;
    }
    nazgulled@nazbox ~ $ cat utf8-ncurses.c 
    #include <ncurses.h>
    
    int main(void) {
            initscr();
    
            // Card suits in black
            char clubs[]    = {0xE2, 0x99, 0xA7, 0};
            char diamonds[] = {0xE2, 0x99, 0xA2, 0};
            char hearts[]   = {0xE2, 0x99, 0xA1, 0};
            char spades[]   = {0xE2, 0x99, 0xA4, 0};
    
            printw("\n%s\n%s\n%s\n%s\n", clubs, diamonds, hearts, spades);
            printw("\náàâãéèêíìîĩóòôõúùûũç\n\n");
            printw("\nÁÀÂÃÉÈÊÍÌÎĨÓÒÔÕÚÙÛŨÇ\n\n");
            getch();
    
            endwin();
    
            return 0;
    }
    Now the following:
    Code:
    nazgulled@nazbox ~ $ gcc utf8-normal.c -o utf8-normal
    nazgulled@nazbox ~ $ gcc -lncurses utf8-ncurses.c -o utf8-ncurses
    nazgulled@nazbox ~ $ ./utf8-normal 
    
    ♧
    ♢
    ♡
    ♤
    
    áàâãéèêíìîĩóòôõúùûũç
    
    
    ÁÀÂÃÉÈÊÍÌÎĨÓÒÔÕÚÙÛŨÇ
    
    nazgulled@nazbox ~ $ ./utf8-ncurses
    
    �~Y�
    �~Y�
    �~Y�
    �~Y�
    
    áàâãéèêíìîĩóòôõúùûũç
    
    
    �~A�~@�~B�~C�~I�~H�~J�~M�~L�~NĨ�~S�~R�~T�~U�~Z�~Y�~[Ũ�~G
    See? Dunno what else to tell you... nor do I know how can I fix this!

  3. #18
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    See? Dunno what else to tell you...
    The terminal copy/paste was actually very informative. That's really what I get for not double checking. The same happens on my box...

    I found this:
    http://mail.nl.linux.org/linux-utf8/.../msg00001.html which seems to contain what you want (and what dwks said earlier). wide chars shouldn't be needed. (why a wide library is needed is still beyond me...) Using what the link contains, I can't set it to work. I have to use a
    Code:
    setlocale(LC_ALL, "en_US.utf-8");
    ...otherwise I get ANSI_X3.4-1968 (ascii) as a character set and not utf-8.

    Also, I'd use the black/solid card symbols (U+2660 + [0, 3, 5, 6]) - I can see them, but not the outline/white ones. You might be having the same problem.

    This worked for me:
    Code:
    #include <locale.h>
    #include <ncursesw/ncurses.h>
    
    int main()
    {
    	setlocale(LC_ALL,"en_US.utf-8");
    	initscr();
    
    	// Card suits in black
    	char clubs[]    = {0xE2, 0x99, 0xA3, 0};
    	char diamonds[] = {0xE2, 0x99, 0xA6, 0};
    	char hearts[]   = {0xE2, 0x99, 0xA5, 0};
    	char spades[]   = {0xE2, 0x99, 0xA0, 0};
    
    	printw("\u2660\n");
    	printw("\n&#37;s\n%s\n%s\n%s\n", clubs, diamonds, hearts, spades);
    	printw("\n&#225;&#224;&#226;&#227;&#233;&#232;&#234;&#237;&#236;&#238;ĩ&#243;&#242;&#244;&#245;&#250;&#249;&#251;ũ&#231;\n\n");
    	printw("\n&#193;&#192;&#194;&#195;&#201;&#200;&#202;&#205;&#204;&#206;Ĩ&#211;&#210;&#212;&#213;&#218;&#217;&#219;Ũ&#199;\n\n");
    	getch();
    
    	endwin();
    
    	return 0;
    }
    Linking with -lncursesw

    Edit: Also, Konsole (xterm?) seems to handle characters wider that 1 space very poorly. Not a problem for card symbols or latin characters that I'm aware of...
    Last edited by Cactus_Hugger; 06-08-2007 at 01:37 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #19
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    Thanks a lot, it's done... Here's a link with a screenshot at what I've done:
    http://img169.imageshack.us/my.php?image=smav9.png

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't recall playing cards with that kind of face design, but it does look VERY good.

    I wonder if that's how they did it back in the DOS days?

    I know they had a way of doing this; have to check out some old books on that.

    Well done!

  6. #21
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Wow. Very nice looking.

    I wonder if that's how they did it back in the DOS days?
    Something similar should be doable in DOS, although not with Unicode. An earlier post suggested casting 3 through 6 to chars, and printing those, in DOS (and Win32 for Windows 98, but I have no idea about later versions) will print card characters. There are Win32 ports of ncurses, so other than changing the character used for the card symbol, most of it should be fairly portable.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What you are doing is awful. Linux console applications traditionally do not mess with the default character set of the terminal. If you can't access the characters via the current codepage, don't *change the codepage* just because you want to use a special glyph. What if the user doesn't even have it?

    Even worse, what if your program has a bug and crashes without resetting the default code page? You've just hosed the user's terminal, and he didn't deserve that.

  8. #23
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    @brewbuck
    I couldn't care less about that... This my code, my project I can do whatever I want with it and besides this is not something to go and release to the public, this is a school project and the teachers only want to read the code and see it working on my laptop so, I don't care about you said cause it's no issue for me...

  9. #24
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Even worse, what if your program has a bug and crashes without resetting the default code page? You've just hosed the user's terminal, and he didn't deserve that.
    To my understanding, this should not happen. Each program gets the locale set to "C", regardless of anyone's settings:
    Quote Originally Posted by man setlocale
    On startup of the main program, the portable "C" locale is selected as default.
    As for not setting the characterset, what is a program supposed to do? ASCII doesn't cover many languages other than English (if any). While spades, diamonds, etc. are a bit off the wall as glyphs go, what about accented latin? What would you have a program do there?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 04:50 PM
  2. Video card mystery
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-30-2003, 02:56 PM
  3. Replies: 2
    Last Post: 11-07-2003, 12:21 AM
  4. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM
  5. Sign up: The Card Game Contest
    By ygfperson in forum Contests Board
    Replies: 40
    Last Post: 09-24-2002, 05:43 PM