Is there a drastic difference between UTF-8 and UTF-16

后端 未结 3 1128
青春惊慌失措
青春惊慌失措 2020-12-25 08:40

I call a webservice, that gives me back a response xml that has UTF-8 encoding. I checked that in java using getAllHeaders() method.

Now, in my java cod

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 09:34

    There are two things:

    • the encoding in which you exchange data;
    • the internal string representation of Java.

    You should not be preoccupied with the second point ;) The thing is to use the appropriate methods to convert from your data (byte arrays) to Strings (char arrays ultimately), and to convert form Strings to your data.

    The most basic classes you can think of are CharsetDecoder and CharsetEncoder. But there are plenty others. String.getBytes(), all Readers and Writers are but two possible methods. And there are all static methods of Character as well.

    If you see gibberish at some point, it means you failed to decode or encode from the original byte data to Java strings. But again, the fact that Java strings use UTF-16 is not relevant here.

    In particular, you should be aware that when you create a Reader or Writer, you should specify the encoding; if you fail to do so, the default JVM encoding will be used, and it may, or may not, be UTF-8.

提交回复
热议问题