Unicode escapes and String literals?

Discussion in 'Java' started by Knute Johnson, Dec 13, 2012.

  1. Knute Johnson

    Lew Guest

    That was not obvious to me, hence my question as to what he did mean.
    That was also the point of my SSCCE.
     
    Lew, Dec 14, 2012
    #21
    1. Advertisements

  2. Knute Johnson

    Roedy Green Guest

    have a look at native2ascii

    IIRC it uses sequences like that in its ASCII representation which you
    can then convert to any encoding you like.

    see http://mindprod.com/jgloss/encoding.html#NATIVE2ASCII

    A little finite state machine should handle that fairly easily.
    If you find that difficult, I would write one for you.
     
    Roedy Green, Dec 14, 2012
    #22
    1. Advertisements

  3. Knute Johnson

    Lew Guest

    This has been explained to death upthread already.

    Those are not Unicode escapes, that's why.

    You have created the String literal that comprises backslashes, the letter "u" and
    various digits. That happens at runtime.

    There is no way for the pre-compiler to see those and convert them.

    That code sequence is exactly equivalent to this one:

    StringBuilder sb = new StringBuilder(\u0031\u0038);
    sb.append('\u005c\u005c\u0027)\u003b
    sb.append("\u0075\u0030\u0030\u0036\u0036");
    sb.append('\u005c\u005c\u0027)\u003b
    sb.append("u006\u0035\u0022);
    sb.append('\u005c\u005c\u0027)\u003b
    sb.append(\u0022\u00750064");

    Unicode escape sequence processing is a pre-compiler operation, not a compiler
    operation and not a run-time operation.

    To do what you want you have to parse the string and convert it yourself.
     
    Lew, Dec 14, 2012
    #23
  4. Knute Johnson

    Arne Vajhøj Guest

    First: it does not do what Knute asked for. It actually
    generates the escape sequences that Knute is trying to
    convert from.

    Second: even it has done whar Knute asked for, then:
    - create a file with the String
    - use Runtime exec (or ProcessBuilder) to run native2ascii
    - read a new String from the new file
    seems at the least efficient solution possible.
    Based on the above: hmmmmmmm.

    Arne
     
    Arne Vajhøj, Dec 15, 2012
    #24
  5. Knute Johnson

    Roedy Green Guest

    The code you want exists inside Quoter.

    see FromJavaStringLiteral
    and ToJavaStringLiteral classes.

    Source is available from http://mindprod.com/products.html#QUOTER
    you can play with it as an Applet at
    http://mindprod.com/applet/quoter.html
     
    Roedy Green, Dec 17, 2012
    #25
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.