String to character literal conversion

D

-D

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
 
A

Alan Moore

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);
 
T

Tor Iver Wilhelmsen

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);
 
T

Thomas Schodt

-D said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top