• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to converst a String to unicode

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic