views:

114

answers:

4

Hi,

I try to convert a UTF8 string to java unicode string.

String question = request.getParameter("searchWord");
byte[] bytes = question.getBytes();
question = new String(bytes, "UTF-8");

The input are Chinese Characters and when I compare the hex code of each caracter it is the same Chinses character. So I'm pretty sure that the charset is UTF8.

Where do I go wrong?

Thx!

+6  A: 

There's no such thing as a "UTF-8 string" in Java. Everything is in Unicode.

When you call String.getBytes() without specifying an encoding, that uses the platform default encoding - that's almost always a bad idea.

You shouldn't have to do anything to get the right characters here - the request should be handling it all for you. If it's not doing so, then chances are it's lost data already.

Could you give an example of what's actually going wrong? Specify the Unicode values of the characters in the string you're receiving (e.g. by using toCharArray() and then converting each char to an int) and what you expected to receive.

EDIT: To diagnose this, use something like this:

public static void dumpString(String text) {
    for (int i = 0; i < text.length(); i++) {
        System.out.println(i + ": " + (int) text.charAt(i));
    }
}

Note that that will give the decimal value of each Unicode character. If you have a handy hex library method around, you may want to use that to give you the hex value. The main point is that it will dump the Unicode characters in the string.

Jon Skeet
告 This character for example needs to be convertedI get 229 145 138 this decimal representation whichis correct according to http://www.ansell-uebersetzungen.com/gbuni.html because it's this hex representation: E5 91 8ASo now I need it to be converted to unicode. I
Rob Hufschmitt
So in my opinion the request sends the right characters but I cannot read these in java, it needs to be converted to unicode
Rob Hufschmitt
@Rob: No, that should appear in the string as U+544A. The hex representation you've quoted is the UTF-8 representation - which is *never* going to be what's in the string itself. You say you "get" 229 145 138 - when you do what? I'll edit my answer with some diagnostic code.
Jon Skeet
+2  A: 

First make sure that the data is actually encoded as UTF-8.

There are some inconsistency between browsers regarding the encoding used when sending HTML form data. The safest way to send UTF-8 encoded data from a web form is to put that form on a page that is served with the Content-Type: text/html; charset=utf-8 header or contains a <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> meta tag.


Now to properly decode the data call request.setCharacterEncoding("UTF-8") in your servlet before the first call to request.getParameter().

The servlet container takes care of the encoding for you. If you use setCharacterEncoding() properly you can expect getParameter() to return normal Java strings.

Alexandre Jasmin
The charset is right in html.
Rob Hufschmitt
Now When I convert I get the representation of unicode 63 for each character So I guess that my conversion is still wrong
Rob Hufschmitt
@Rob You shouldn't have to make any manual conversion. You should call `setCharacterEncoding("UTF-8")` and use `request.getParameter()` to get a normal Java Unicode string. I suppose your code works with normal ascii characters as well?
Alexandre Jasmin
And please use @Jon Skeet code snippet to get the Unicode code point of each character instead of `String.getBytes()`.
Alexandre Jasmin
@Alexandre Jasmin: Thank you so much, you really made my day!
Rob Hufschmitt
You're welcome.
Alexandre Jasmin
A: 

Also you may need a special filter which will take care of encoding of your requests. For example such filter exists in spring framework org.springframework.web.filter.CharacterEncodingFilter

endryha
A: 
String question = request.getParameter("searchWord");

is all you have to do in your servlet code. At this point you have not to deal with encodings, charsets etc. This is all handled by the servlet-infrastucture. When you notice problems like displaying �, ?, ü somewhere, there is maybe something wrong with request the client sent. But without knowing something of the infrastructure or the logged HTTP-traffic, it is hard to tell what is wrong.

Michael Konietzka