Coding problems have been bothering me, every time I encountered garbled or coding problems, online A check, the problem solved, but the actual principle did not understand, each encounter, is what headache.
decided to thoroughly end the one-time solution coding problem.
1. Why to encode
The basic unit of a computer is a byte, and a byte is 8bit. The range of one byte is 0~255.
Humans must represent more than 256 symbols, so it is not possible to use a single byte to represent the many symbols.
You think, there are thousands of characters in light.
To resolve this contradiction, there is a new data structure: char. Char is the character, the longest 16bit, and the shortest is 8bit. The maximum length of a character is 16bit. The range of one character is 0~2 16 times.
So a char-type data structure can represent all the characters on this earth.
But the computer will not change for you, its basic unit or byte, 8bit. So how to represent a char character up to 16bit as one or several 8bit long byte bytes,
This process requires coding.
2. How to Translate
All language communication needs to be translated. A char character that is 16bit long is represented as one or several 8bit long byte bytes,
Many translations are available on the computer: ascii,iso-8859-1,gb2312,gbk,utf-8,utf-16
The above several coding methods can be regarded as a dictionary, they specify the rules of conversion, according to this rule can let the computer correctly through the byte to represent our own defined character char this data type.
ASCII code:
ASCII code, a total of 128. Since the ASCII code is only 128, it can be represented by a 8bit long byte, or by a 16bit long character.
0~31 represents control characters such as newline, enter, delete, etc., 32~126 are print characters such as uppercase letters, lowercase letters, etc.
The ASCII code is a single-byte encoding, which is a 8bit long char character or a 8bit byte character encoded in one byte.
Iso-8859-1:
128 characters is enough for a country that only uses English, but it is not enough for the languages of the Western European countries, because the language of their country, in addition to the English alphabet, there are other characters.
The ISO organization, on the basis of the ASCII code, developed a series of standards to extend the ASCII code, which are iso-8859-1 to iso-8859-15. Where Iso-8859-1 covers most of the Western European language characters,
So the most widely used.
Iso-8859-1 is also a single-byte encoding, which is a 8bit long char character encoded in one byte. The iso-8859-1, which extends the ASCII code, can represent a total of 256 characters.
GB2312:
The full name of GB2312 is the Chinese coded character set of information technology, GB2312 is a double-byte encoding, which is a 16bit long char character encoded in two bytes.
GB2312 can represent 682 symbols and 6,763 kanji.
GBK:
GBK is the full name of the "Chinese character Code extension Code", its appearance is to expand the GB2312, to represent more Chinese characters.
GBK is a double-byte encoding, which is a 16bit long char character encoded in two bytes.
GBK can represent 21,003 characters.
UTF-16:
When it comes to UTF, it's important to mention that Unicode,iso is trying to create a new hyper-language dictionary that all the languages in the world can translate to each other through this dictionary, which is Unicode.
UTF-16 and the UTF-8 mentioned below are all different implementations of Unicode.
UTF-16 is a double-byte encoding, which is a 16bit long char character encoded in two bytes.
UTF-8:
UTF-16 Unified use of two bytes to represent a character, although the presentation is very simple, convenient, but there are shortcomings, there are a lot of characters with a byte can be expressed, but the use of two bytes to represent, resulting in
Waste of storage space. UTF-8 uses a variable-length representation technique, with different character lengths for each coded area. Different types of characters can be represented by 1~6 bytes.
UTF-8 has the following coding rules:
If it is a byte or 8bit long char, the highest bit is 0, it means that this is an ASCII character and UTF-8 is represented by a single byte.
If a byte starts with 11, then the number of consecutive 1 indicates the number of bytes in the character. 110XXXXX indicates that this is the first byte of the char character, which consists of two bytes, this char character is 16bit long,
UTF-8 requires two bytes or more bytes to encode the character.
If a byte starts with 10, the table name is not the first byte of the char character, the previous byte is the first byte of the char character, and it implies that the char character is made up of two bytes, which is a char character of 16bit long,
UTF-8 needs to encode this character using two or more bytes.
3.
In-depth analysis of coding issues in the Java Web