problem printing unicode in java

S

soph

I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values. I've been trying to
do it by appending strings, i.e. character = "\\u" + "7557", but when
I print this it prints exactly "\u7557" instead of the character that
that should represent. Any suggestions?

Thanks!
 
A

Arne Vajhøj

soph said:
I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values. I've been trying to
do it by appending strings, i.e. character = "\\u" + "7557", but when
I print this it prints exactly "\u7557" instead of the character that
that should represent. Any suggestions?

(char)Integer.parseInt("7557", 16)

must be one way.

Arne
 
J

Joshua Cranmer

soph said:
I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values. I've been trying to
do it by appending strings, i.e. character = "\\u" + "7557", but when
I print this it prints exactly "\u7557" instead of the character that
that should represent. Any suggestions?

As Mr. Vajhøj already said, (char)Integer.parseInt(hexCode, 16) would work.

Note that the \u**** codes are actually processed by the compiler before
the lexical scanning process, which can get you some fun discoveries:

jcranmer@quetzalcoatl ~ $ cat JLSTidbit.java
public class JLSTidbit {
public static void main(String... args) {
System.out.print("Line\n");
// \n == \u000A
System.out.print("Line\u000A");
}
}
jcranmer@quetzalcoatl ~ $ javac JLSTidbit.java
JLSTidbit.java:5: unclosed string literal
System.out.print("Line 2\u000A");
^
JLSTidbit.java:5: unclosed string literal
System.out.print("Line 2\u000A");
^
2 errors
 
S

Stefan Ram

soph said:
I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values

There are no »hex values« - there are »hex numerals« and
»int values«.
Any suggestions?

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( new java.lang.String
( new int[]{ java.lang.Integer.parseInt( "41", 16 )}, 0, 1 )); }}

A
 
J

John B. Matthews

[Please trim signatures when responding.]
I get an error if i try to append just '\u'

Yep. "If an eligible \ is followed by u, or more than one u, and the
last u is not followed by four hexadecimal digits, then a compile-time
error occurs [Ibid.].

Joshua Cranmer offered some additional interesting finding, above.
 
K

Knute Johnson

soph said:
I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values. I've been trying to
do it by appending strings, i.e. character = "\\u" + "7557", but when
I print this it prints exactly "\u7557" instead of the character that
that should represent. Any suggestions?

Thanks!

I'm curious, what font are you using?

Thanks,
 
K

Knute Johnson

Arne said:
Are there any font where this is not the case?

Arne

Arne:

No. I was just curious what font would have these characters. I don't
have any such fonts and I thought I could maybe find one somewhere if I
knew what it was.
 
R

RedGrittyBrick

Knute said:
Arne:

No. I was just curious what font would have these characters. I don't
have any such fonts and I thought I could maybe find one somewhere if I
knew what it was.

u7557 is one of the CJK unified ideographs. Many fonts have it. For
example, Arial Unicode MS and Bitstream Cyberbit. I assume many fonts in
use in the far east must include it.
 
K

Knute Johnson

RedGrittyBrick said:
u7557 is one of the CJK unified ideographs. Many fonts have it. For
example, Arial Unicode MS and Bitstream Cyberbit. I assume many fonts in
use in the far east must include it.

Thanks RGB. Believe or not, they charge $100 for Arial Unicode but
include it in MS Office.
 
R

Roedy Green

I need to print some Chinese characters in unicode, but I need to
generate them dynamically from their hex values. I've been trying to
do it by appending strings, i.e. character = "\\u" + "7557", but when
I print this it prints exactly "\u7557" instead of the character that
that should represent. Any suggestions

\uxxxx notation is what you use in text to feed to the Java compiler.
It is gone by the time your code is running.

to create them on the fly you would say things like

char c = '\u7757'; // note single quote

char c = (char) 0x7757;

char c = (char)30551; //0x7757 in decimal

char c = (char) someInt;


--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 

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,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top