I am programming converting chinese string to byte array and byte array to chinese string. I know when using UTF-8 encode, chinese string return 2 bytes. But It returns 3 bytes in my computer.
//test code
String result = System.getProperty("file.encoding"); // UTF-8
String temp = new String("中国");
byte[] bytes = temp.getBytes();
Result:
bytes = {-28,-72,-83,-27,-101,-67}
But I don'k know well. Please help me. Thanks
I'm not sure where you're getting the assertion that Chinese characters are all 2 bytes in UTF-8.
中 is U+4E2D, and 国 is U+56FD. If you look at the summary of how many bytes each code point takes in UTF-8 , you'll see that code points between U+0800 and U+FFFF (inclusive) require 3 bytes. Both of the characters here fall in that range.
For instance, 中 (U+4E2D) encodes as 11100100 10111000 10101101 (you can play with UTF8-to-binary conversions here ), and those three bytes do in fact translate to -28, -72 and -83 in twos complement (you can play with decimal-to-two's complement here ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.