Results 1 to 9 of 9

Thread: Unicode

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    117
    Rep Power
    0

    Default Unicode

    Hello!

    I'm writing a card game called "Twenty-one", and for that I wanted the card symbols (Hearts, diamonds etc.) which are available in Unicode. So I filled a Vector<String> with cards and with the symbols which were presented on Unicode's website, but when I try to output them I only get a question mark instead of the card symbols. So I'm wondering if I need to import something to be able to see them. I'm using Eclipse btw. Here is my code:

    Java Code:
    Vector<String> cards = new Vector<String>();
    cards.addAll(Arrays.asList("\u2664 E", "\u2664 2", "\u2664 3", "\u2664 4", "\u2664 5", "\u2664 6", "\u2664 7", "\u2664 8", "\u2664 9", "\u2664 10", "\u2664 Knight", "\u2664 Queen", "\u2664 King",
    					       "\u2665 E", "\u2665 2", "\u2665 3", "\u2665 4", "\u2665 5", "\u2665 6", "\u2665 7", "\u2665 8", "\u2665 9", "\u2665 10", "\u2665 Knight", "\u2665 Queen", "\u2665 King",
    					       "\u2666 E", "\u2666 2", "\u2666 3", "\u2666 4", "\u2666 5", "\u2666 6", "\u2666 7", "\u2666 8", "\u2666 9", "\u2666 10", "\u2666 Knight", "\u2666 Queen", "\u2666 King",
    					       "\u2667 E", "\u2667 2", "\u2667 3", "\u2667 4", "\u2667 5", "\u2667 6", "\u2667 7", "\u2667 8", "\u2667 9", "\u2667 10", "\u2667 Knight", "\u2667 Queen", "\u2667 King"));

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Unicode

    Where are you printing the results? Eclipse console? Try in cmd line. Or writing it out to a file and viewing it in notepad.

    Edit:

    Did some research, found out Java is a little odd with different encodings. Try doing it this way:

    String str = new String(new byte[] { (byte)'\u2664', ' ', 'E' }, "utf-8");
    System.out.println(str);

    And see if it suits your needs. Not very nice looking... I'm sure there is a better way to do it, though.
    Last edited by AndrewM16921; 09-21-2013 at 05:14 AM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Unicode

    What happens if you print the following in your Eclipse console? (it prints fine for me)

    Java Code:
    System.out.println("\u2664 E");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    117
    Rep Power
    0

    Default Re: Unicode

    @ AndrewM16921 I'm printing the results in the Eclipse console, yes.
    I will try doing that, thanks :) It is going to be tedious doing it for every element in my string vector though...

    @ JosAH It just prints the \u2664 part as a question mark, but the rest is fine. Strange thing though is that when I run the program in BlueJ instead, it works fine.
    Last edited by Zelaine; 09-21-2013 at 10:40 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Unicode

    (byte)'\u2664'
    Converting a 2 byte Unicode char to a byte would drop the upper half of the bits in the character.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Unicode

    Quote Originally Posted by Norm View Post
    Converting a 2 byte Unicode char to a byte would drop the upper half of the bits in the character.
    Oh true x_x good point

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    117
    Rep Power
    0

    Default Re: Unicode

    So how does one make them visible then? They're visible in BlueJ, but strangely enough not in Eclipse...

  8. #8
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Unicode

    Window > Preferences > General > Workspace > set text file encoding to UTF-8.

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    117
    Rep Power
    0

Similar Threads

  1. Help in Unicode
    By justbeller in forum NetBeans
    Replies: 0
    Last Post: 01-29-2011, 06:43 PM
  2. wat is the unicode of '?' ? 63 or 128
    By Jagdeesh123 in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 08:49 AM
  3. Unicode(UTF-8) to PDF
    By karasatishkumar in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2010, 10:59 AM
  4. Lucene with unicode
    By goms in forum Lucene
    Replies: 1
    Last Post: 02-02-2010, 01:20 PM
  5. SWT 2D Unicode Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •