1. Java Encoding method
reference 1:http://blog.csdn.net/clbxp/article/details/6625142
Java uses the Unicode character set, which uses UTF-16 encoding in memory: one character occupies 2 bytes, and one common Chinese character occupies 2 bytes, but the characters in the very large coded character set are 4 bytes. That
UTF-16 encoded Form
Category |
Number of characters |
Number of bytes |
Bit bits |
Character |
1 |
2 |
16 |
Common Chinese characters |
1 |
2 |
16 |
Large character set (uncommon characters, etc.) |
2 |
4 |
32 |
Byte is a byte, and the string needs to be converted to a byte array before the network is transmitted or stored. A byte array needs to be converted to a string after it is received from the network or read from the storage device. String is a string that can be viewed as an array of char. String and char are memory forms, and byte is the serialized form of network transport or storage.
string serialization into a byte array or deserialization needs to choose the correct encoding method。 If the encoding is not correct, you will get some 0x3f values. The commonly used character encoding methods are Iso8859_1, GB2312, GBK, utf-8/utf-16/utf-32.
2, String reversal reference: Http://www.jb51.net/article/37399.htm Inversion method StringBuilder in the parent class, the source code is as follows:
public Abstractstringbuilder reverse () {Boolean hassurrogate = false; int n = count-1; for (int j = (n-1) >> 1; J >= 0;--j) {Char temp = value[j]; Char temp2 = value[n-j]; if (!hassurrogate) {hassurrogate = (temp >= character.min_surrogate && temp <= Character.max_surrogat E) | | (Temp2 >= character.min_surrogate && temp2 <= character.max_surrogate); } Value[j] = Temp2; VALUE[N-J] = temp; } if (hassurrogate) {//Reverse back all valid surrogate pairs for (int i = 0; i < count-1; i++ ) {char C2 = value[i]; if (character.islowsurrogate (C2)) {Char C1 = value[i + 1]; if (Character.ishighsurrogate (C1)) {value[i++] = C1; Value[i] = C2; }}}} return this; }
*************************************************************************************************************** ****My Implementation:
/** * Created: August 15, 2014 PM 9:44:51 * Project name: Test * @author Cao yanfeng * @since JDK 1.6.0_21 * Class Description: * /public class Accuracytest { /** * @param args */public static void Main (string[] args) { //TODO auto-generated method Stub String string= "Caoyanfeng Cao Yanfong Peking University