Hello, if I have a string A, how I can see the string's unicode? say,
display the String with format: "\u611F\u5192";
Currently I have a string A, which I don't know the encoding, but if I
display that string in the firefox browser, it is just some strange
letters, like this "æå". Actually is should display some meaningful
chinese characters, like "感冒"。
Can any one tell me how to convert the string A into unicode format
and let it display correctly in the browser?
Thanks a lot!
RedGrittyBrick - 14 Nov 2007 21:39 GMT
> Hello, if I have a string A, how I can see the string's unicode? say,
> display the String with format: "\u611F\u5192";
[quoted text clipped - 6 lines]
> Can any one tell me how to convert the string A into unicode format
> and let it display correctly in the browser?
Just tell the browser it is UTF8
HTTP:
Content-Type: text/html; charset=utf-8
HTML:
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
XHTML:
http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020430/#text-html
Roedy Green - 15 Nov 2007 23:43 GMT
On Wed, 14 Nov 2007 19:55:06 -0000, "au.danji@gmail.com"
<au.danji@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>Can any one tell me how to convert the string A into unicode format
>and let it display correctly in the browser?
What you are asking in how to convert a String to char[]. From there
you want to dump each char in hex, and prefix a decorative \u
See http://mindprod.com/jgloss/conversion.html
http://mindprod.com/jgloss/hex.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
John O'Conner - 16 Nov 2007 05:58 GMT
> Hello, if I have a string A, how I can see the string's unicode? say,
> display the String with format: "\u611F\u5192";
>
> Currently I have a string A, which I don't know the encoding,
If you mean a Java String, then the String A is in Unicode, represented
in the API as UTF-16 code units.
but if I
> display that string in the firefox browser, it is just some strange
> letters, like this "æå". Actually is should display some meaningful
> chinese characters, like "感冒"。
Usually this is a combination of font choice and charset declaration.
Make sure you 1) declare the charset encoding of the HTML file and
generate the appropriate character code units for the HTML, and 2)have a
font that can contains glyphs for the Chinese characters.
> Can any one tell me how to convert the string A into unicode format
> and let it display correctly in the browser?
The String A is already a Unicode encoding -- UTF-16. Use the
String#getBytes method to get a byte[] in a different encoding.
Regards,
John O'Conner
Arne Vajhøj - 17 Nov 2007 17:29 GMT
> Hello, if I have a string A, how I can see the string's unicode? say,
> display the String with format: "\u611F\u5192";
public static String encode(String s) {
StringBuffer sb = new StringBuffer("");
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if((c >= 0) && (c <=255)) {
sb.append(c);
} else {
String hex = Integer.toHexString(c);
sb.append("\\u" + "0000".substring(hex.length(), 4) + hex);
}
}
return sb.toString();
}
Arne