This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to converst a String to unicode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to converst a String to unicode" Watch "How to converst a String to unicode" New topic
Author

How to converst a String to unicode

John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Hllo There,
I have to a need to convert a special character to a unicode but don't know how.

I see a unicode to a character but not the other way around. For example:

String original = new String("A" + "\u00ea" + "\u00f1"
+ "\u00fc" + "C");

becomes: A���C

but when I don't know how to convert � this to "\u00ea". Can someone show me please? Thanks.

John
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

Java "String" are unicode. Can you try rewording your request? If you have a String, its unicode. If you want to insert a special character, you look up the character and escape it, exactly the way you show in your question.

What is your question?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

You want to convert the string "\u00ea" (which has 1 character) to the string "\\u00ea" (which has 6 characters)? Start with this:
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Thanks to Paul and Pat for your kindness. It helps me a lot specially dealing with the hex.

If If have a numerical value of an int 192, its hex value is c0. How do I convert this numerical value into a special character?

For value of 256 or higher, when I cast that number to char, I just see [?]. I wonder that I have to set some sort of UTF8 but don't know how. Could you show me how to set the output stream? Thank you very much. You all have given a big help.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
What chars with values > 255 can you display on your system? What's displayed might depend on the fonts and charsets you have.
I think I'm pretty much limited to ASCII (ie 0-255), but I haven't tried.

You should be able to cast int to char for the max of the 2 bytes of char.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

If you want to verify characters outside your default character set, you can check them with Swing:Which characters you can display in a console depends on your environment. A modern Linux distribution will use UTF-8 for xterm, etc., but I don�t think Windows consoles can display Unicode at all.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

Originally posted by John McDonald:
If If have a numerical value of an int 192, its hex value is c0. How do I convert this numerical value into a special character?

For value of 256 or higher, when I cast that number to char,


The upthread example shows how to do general unicode special characters.


just replace the 00ea with whatever you need.

Seeing Unicode is another matter. You have to have the proper language character fonts installed, and you have to know how to read them. For 90% of the world's languages that don't use something Roman, I have no idea what a character is supposed to look like.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

When you had that "c0" which you wanted to convert to Unicode, did you have any idea of what result you should be expecting? It sort of sounds to me like you're fishing in the dark. Go and have a look through the Unicode code charts. Start by looking at the ones named "Latin something".
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
My question was if I have a value of 192, how do I change this value of special A character. So the answer is

int value = 192;
String specialCharacter = "" + (char)value;
System.out.println("special A[" + specialCharacter.charAt(0) + "]");
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

That's right, the concise answer is "just cast it to a char". I just wanted to make sure you really had Unicode in mind. Many people think that some extended version of ASCII is what Java uses, and they get confused when they don't see the character they expect when they do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to converst a String to unicode
 
Similar Threads
problem with unicode strings in dynamic jsps
characters to unicode
System.out.printf("%1$c",65536);
char value
Unicode Character