×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Super Contributor
    Join Date
    Mar 2004
    Location
    Bangalore,India
    Posts
    2,146

    Issue with Unicode literals

    Hi,

    I am encountering an issue where if my string has Unicode literals like \u000a(linefeed), \u000d, \u0009(for tab), on the S40 devices like 6300 it ends up truncating/missing out some of the text while displaying. However the same string literal works on the Default ColorPhone Emulator but it still doesn't show the linefeed/tab etc.

    I believe this has something to do with http://www.jguru.com/faq/view.jsp?EID=225894.

    But my doubt is how come on the device it ends up truncating/cutting the text randomly, while on the emulator the Unicode is completely ignored?
    Cheers,
    Mayank

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Issue with Unicode literals

    First, I suggest you don't use \uxxxx notation for control characters string or character literals. Use \n (newline), \r (carriage return) and \t (tab). Otherwise, you might have compiler issues.

    "\u" elements are translated much earlier than other "escapes". They are converted before the compiler sees the code. For example:

    Code:
    System\u002eout\u002eprintln("this works");
    is quite legal. '\u002e' is considered identical to '.' in all situations. This is also legal:

    Code:
    System.out.print("this works\n");
    But the following is not.

    Code:
    System.out.print("this will not compile\u000a");
    Graham.

  3. #3
    Super Contributor
    Join Date
    Mar 2004
    Location
    Bangalore,India
    Posts
    2,146

    Re: Issue with Unicode literals

    Thanks for the quick reply.

    I tried out those combination myself and realized it does throw compile time error, because of the explanation given in the Java language specification as
    "Because Unicode escapes are processed very early, it is not correct to write
    '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape
    \u000a is transformed into an actual linefeed in translation step 1 (§3.3) and the
    linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is
    not valid in step 3. Instead, one should use the escape sequence '\n' (§3.10.6).
    Similarly, it is not correct to write '\u000d' for a character literal whose value is
    carriage return (CR). Instead, use '\r'.
    However my other doubt then is:-

    1.How come the behavior is different on the Device vs the emulator in this case, does it have anything to do with the JVM implementation etc?
    2. What happens if at runtime one of my string has a value of something like "line 1\u000aline 2\u000a\u000aline 4", and now if I do a System.out.println of this string, since its past the compile time stage it wont complain of an error, but how does it actually render it?
    Cheers,
    Mayank

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Issue with Unicode literals

    Well, I don't know what's in your code. Presumably, you don't have:

    Code:
    String myString = "line 1\u000aline 2\u000a\u000aline 4";
    This would not compile.

    And I don't know how you are displaying the strings. If you are displaying them on a Form or List, different implementations might handle multi-line strings differently. You should check the MIDP specification carefully for what an implementation MUST do, and what it MAY do (but might not).

    Graham.

  5. #5
    Super Contributor
    Join Date
    Mar 2004
    Location
    Bangalore,India
    Posts
    2,146

    Re: Issue with Unicode literals

    Quote Originally Posted by grahamhughes View Post
    Well, I don't know what's in your code. Presumably, you don't have:

    Code:
    String myString = "line 1\u000aline 2\u000a\u000aline 4";
    This would not compile.
    You are right I dont have it that ways, I have a file which feeds me the content as "line 1\u000aline 2\u000a\u000aline 4", which I am then reading into a string.

    Quote Originally Posted by grahamhughes View Post
    And I don't know how you are displaying the strings. If you are displaying them on a Form or List, different implementations might handle multi-line strings differently. You should check the MIDP specification carefully for what an implementation MUST do, and what it MAY do (but might not).
    Graham.
    I am using a low-level canvas based control which I have developed myself to render the text using the good ole DrawString methods.
    Cheers,
    Mayank

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Issue with Unicode literals

    Graphics.drawString() will not usually honour line breaks in the text. If you want text broken over several line, you should make several drawString() calls, one for each line.

    Graham.

Similar Threads

  1. A question about isalpha with Unicode
    By gloria799 in forum Symbian
    Replies: 1
    Last Post: 2007-11-14, 22:18
  2. Devenagri Unicode Font for Nokia 6600
    By gapata in forum Series 40 & S60 Platform Feedback Archive
    Replies: 1
    Last Post: 2007-01-22, 09:59
  3. unicode with midp2 utf-8
    By kmsoomro in forum Mobile Java General
    Replies: 0
    Last Post: 2006-02-16, 10:31
  4. Replies: 3
    Last Post: 2005-08-01, 13:39
  5. Unicode characters
    By Nokia_Archive in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2002-05-17, 14:09

Posting Permissions

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