Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: \u escape character not working

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default \u escape character not working

    Hello all,

    In a java program I'm trying to display the degree symbol in the command prompt using println(), but it's not displaying properly. Here is the statement:

    System.out.println( "\u00b0" );

    Thanks for any suggestions.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: \u escape character not working

    Quote Originally Posted by nicnicman View Post
    ... it's not displaying properly...
    A couple of questions:
    1. Is it displaying anything? Anything at all?
    2. Are you running from an Operating System terminal command line? Or from a "command line" inside an IDE like Netbeans or Eclipse?
    3. What happens if you print ("\260")? That is the single octal byte corresponding to hex 0xb0.


    From my Linux system operating from a gnome-terminal command line, both show me a "degree" symbol in the output.
    I'm not sure how it shows up on browsers around the world, but...
    public class Z {
        public static void main(String [] args) {
            System.out.println("Here it is:");
            System.out.println( "\u00b0" );
            System.out.println("There it was.");
            System.out.println("Here it is again:");
            System.out.println("\260");
            System.out.println("There it was again.");
        }
    }

    Output, pasted from my gnome terminal:
    Here it is:
    °
    There it was.
    Here it is again:
    °
    There it was again.

    When I redirected the output to a file, here's the hex dump of the file:
    00000000  48 65 72 65 20 69 74 20  69 73 3a 0a c2 b0 0a 54  |Here it is:....T|
    00000010  68 65 72 65 20 69 74 20  77 61 73 2e 0a 48 65 72  |here it was..Her|
    00000020  65 20 69 74 20 69 73 20  61 67 61 69 6e 3a 0a c2  |e it is again:..|
    00000030  b0 0a 54 68 65 72 65 20  69 74 20 77 61 73 20 61  |..There it was a|
    00000040  67 61 69 6e 2e 0a                                 |gain..|
    00000046

    Both lines result in hex bytes 0xc2, 0xb0 being deposited in the file. Note that I can also print the "degree" symbol by executing the following from a command line:
     echo -e "\0302\0260"

    That puts out the characters 0xc20xb0

    Bottom line: Anything I can print from a command line on my system can be done with println() from Java. (java 1.6.0_22)

    This particular system is "plain vanilla" Centos 5.8 Linux (directly descended from RedHat source they say) with the default US locale installation stuff.

    I'm not sure whether (or how) any kind locale environment variables have anything to do with how unicode is displayed. Maybe someone from our large international community can enlighten us...


    Cheers!

    Z
    Last edited by Zaphod_b; October 14th, 2012 at 02:41 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: \u escape character not working

    Thanks for the response.

    It displays as series of diagonal lines in the shape of a box. Here is a screen shot:



    I'm running it in Windows 7 command prompt from the text editor TextPad .

    It displays the same when I use ("\260").
    Last edited by nicnicman; October 14th, 2012 at 02:55 PM.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: \u escape character not working

    Quote Originally Posted by nicnicman View Post
    ...

    I'm running it in Windows 7 command prompt from the text editor TextPad .
    Can't help directly with Windows 7, but here's the kind of thing I used to do with Windows XP:

    Get out of TextPad and run the command line program from a command line. Sometimes editors are set up to use a different Code Page than what you see from a "raw" command line.


    Try all characters below 256 to see if any look like what you want to see:
    import java.util.*;
     
    public class Z {
        public static void main(String [] args) {
            char degreeChar = '\u0000';
            for (int i = 0; i < 256; i++) {
                System.out.printf("0x%02x (%d): %c\n", i, i, degreeChar);
                ++degreeChar;
            }
     
        }
     
    }

    Scroll back through the output and see if anything looks like the symbol that you want. For me, 0xf8 (decimal 248) looks like a winner.

    So, I change the program a little to create an actual output line that I want to "look good" in the "real" application.
    import java.util.*;
     
    public class Z {
        public static void main(String [] args) {
            char degreeChar = '\u0000';
            for (int i = 0; i < 256; i++) {
                System.out.printf("0x%02x (%d): %c\n", i, i, degreeChar);
                ++degreeChar;
            }
     
            // Visually selected from screen output on Windows XP
            degreeChar = '\u00f8';
            System.out.printf("\nUsing degreeChar = (int)0x%04x: %c\n\n", 
            (int)degreeChar, degreeChar);
            double degF = 75.0;
            double degC = (degF - 32.0) / 1.8;
            System.out.printf("%.2f %cF = %.2f %cC\n", degF, degreeChar, degC, degreeChar);
        }
     
    }

    I have attached a screen shot of the last 23 (or so) lines of output from the program.

    Bottom line: IWFMYMMV (It Works For Me; Your Mileage May Vary.)


    Cheers!

    Z
    Attached Images Attached Images
    Last edited by Zaphod_b; October 14th, 2012 at 04:30 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    nicnicman (October 14th, 2012)

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: \u escape character not working

    I've never really had much luck with characters outside the standard 127 ASCII encoding in Windows command prompt...

    You can use the chcp command to change the character encoding, but I've had mixed results with this. The documentation also isn't great, the best place I've found for information on what number corresponds to what encoding is Wikipedia, and I suspect this may even be incorrect.


    edit:

    Funny enough I checked the encoding Java was using and in Eclipse it launches with utf-8, in the console it launches with windows-1252. I'll do a little more investigation to see if I can figure out what's going on here...

    I also found better documentation from Microsoft.

    The only really reliable solution I found was to use windows-1252 (chcp 1252). For some reason using UTF-8 encoding with chcp 65001 is giving me extra characters...

    In command-prompt you can change the encoding to UTF8 using -Dfile.encoding=UTF8, or to windows-1252 using -Dfile.encoding=windows-1252.
    Last edited by helloworld922; October 14th, 2012 at 06:18 PM.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    jps (October 14th, 2012)

  8. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: \u escape character not working

    Zaphod, I went through the process you presented and came up with the same thing you did: \u00f8. Btw, it works great!
    Thanks a lot.

Similar Threads

  1. [SOLVED] tokenizing a string with escape sequence
    By nosxlimit in forum Java Theory & Questions
    Replies: 9
    Last Post: July 26th, 2012, 12:30 AM
  2. Question to Pattern.matches and escape sequences
    By steppenwolf in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 04:29 PM
  3. Using \n escape sequence in a toString method
    By coolidge in forum Java Theory & Questions
    Replies: 4
    Last Post: September 22nd, 2011, 04:14 PM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM