String to character literal conversion

Discussion in 'Java' started by -D, Sep 4, 2004.

  1. -D

    -D Guest

    Hi all,

    How do you evaluate a string as unicode literal? That is,
    how do you convert "\u004E" (normal string) to '\u004E' (character literal)?

    Thanks!

    - D
     
    -D, Sep 4, 2004
    #1
    1. Advertising

  2. -D

    Alan Moore Guest

    On 3 Sep 2004 17:41:02 -0700, (-D) wrote:

    >Hi all,
    >
    >How do you evaluate a string as unicode literal? That is,
    >how do you convert "\u004E" (normal string) to '\u004E' (character literal)?
    >


    char ch = (char)Integer.parseInt("\\u004E".substring(2), 16);
     
    Alan Moore, Sep 4, 2004
    #2
    1. Advertising

  3. (-D) writes:

    > How do you evaluate a string as unicode literal? That is,
    > how do you convert "\u004E" (normal string) to '\u004E' (character literal)?


    If it's in source code, the resulting String will have 1 character;
    you get that with

    char ch = theString.charAt(0);
     
    Tor Iver Wilhelmsen, Sep 4, 2004
    #3
  4. -D wrote:

    > How do you evaluate a string as unicode literal? That is,
    > how do you convert "\u004E" (normal string) to '\u004E' (character literal)?


    The only way I can make any sense of your post is if you mean


    public class Eval {
    // note the double backslash
    static final String normalString = "\\u004E";
    static final char literalCharacter = evaluateString(normalString);

    // argument must contain "\\uXXXX" exactly
    // first is a backslash '\\', second is a lowercase 'u'
    // ande XXXX is a unicode character id
    static char evaluateString(String s) {
    // insert code here
    return (char)Integer.parseInt(s.substring(2),0x10);
    }

    public static void main(String[] arg) {
    System.out.println(literalCharacter);
    }
    }


    Improving the evaluateString() to take arguments of varying format
    is left as an exercise for the OP.
     
    Thomas Schodt, Sep 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    763
    those who know me have no need of my name
    May 28, 2006
  2. Jef Driesen
    Replies:
    15
    Views:
    3,890
    Default User
    Mar 26, 2010
  3. Jef Driesen
    Replies:
    9
    Views:
    2,927
    Default User
    Mar 26, 2010
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    401
    Anonieko Ramos
    Sep 27, 2004
  5. Water Cooler v2

    Insert a new line character into a string literal

    Water Cooler v2, Apr 18, 2006, in forum: Javascript
    Replies:
    4
    Views:
    384
    Michael Winter
    Apr 18, 2006
Loading...

Share This Page