Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to converst a String to unicode

 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic