json_encodeMethods when encoding Chinese, no parameters are added
JSON_UNESCAPED_UNICODEGet a list of classes
\uXXXXThe string with parameters is the Chinese we usually see. What happened?
//1.php <?php echo json_ Encode ('good '); # php 1.php > 1.txt # ls -l 1.txt -rw-r--r-- 1 root root 8 Jun 12 15:21 1.txt # cat 1.txt "\u597d"
//2.php <?php echo json_ Encode ('good ', JSON_ UNESCAPED_ UNICODE); php 2.php > 2.txt # ls -l 2.txt -rw-r--r-- 1 root root 5 Jun 12 15:23 2.txt # cat 2.txt "Good"
Let’s start with the conclusion
We usually use
jsonThe format is
utf-8Code, but it recognizes
utf-16The escape of encoding. That is,
\u597dWhen a single character (there are six) is picked up and transmitted, it is also true
utf-8Code, but in the specific analysis, it is judged that there is an escape
dThese six characters together, as
JSON_UNESCAPED_UNICODE, no character
utf-16Escape, direct use
utf-8。 Since no escape is used, the entire string size is reduced from 8 bytes to 5 bytes. The reason is the escape character
\u597dEach character takes up 1 byte, which is 6 bytes in total
utf-8The encoding is only 3 bytes, less than 3 bytes.
hexdumpYou can view text files in binary format. The following
-cParameter, if the corresponding single byte has a
asciiCode, will directly
asciiCode format display, otherwise display with corresponding octal number;
-bParameter, which displays each byte completely in octets.
# hexdump -c 1.txt 0000000 " \ u 5 9 7 d " 0000008 //Octal format # hexdump -b 1.txt 0000000 042 134 165 065 071 067 144 042 0000008 # hexdump -c 2.txt 0000000 " 345 245 275 " 0000005 //Octal format # hexdump -b 2.txt 0000000 042 345 245 275 042 0000005
Use of the above
od -w1 -b 2.txtThere are similarities
hexdumpAlmost the same effect
It is not difficult to see from the above output,
goodThe corresponding byte octal number should be
Convert to binary, respectively
Binary can also be converted into hexadecimal numbers, but there is a concern about the size of byte order. Which end starts
345Is it the beginning?
The breakthrough is
goodIt’s 3-byte, 3-byte
utf-8The beginning of the code must be
1110(see Ruan Yifeng’s note on character encoding: ASCII, Unicode and UTF-8)
345And the inner byte is also the big end. After determining the big end, it is associated with the often said network byte order is the big end. Looking at the JSON standard,
utf-32There is a distinction between the big and the small
jsonIt should be big end. (I can’t figure out how to judge multi byte characters if it’s a small end.)
Convert binary to hexadecimal, respectively
Find an online code conversion verification to verify whether there is a wrong conversion
|character||Coded decimal||Encoding hexadecimal||Unicode encoding decimal||Unicode encoding hexadecimal|
E5a5bd is aligned.
Another online code conversion site gets the result
好This is actually
Unicode code pointIt’s not what you want
utf-8Coding, seriously misleading people, many online coding websites are like this.
jsonIt is a transmission protocol that specifies a text organization structure for interaction.
utf-8Code, but not necessarily.
unicodeIs a standard that defines a unique correspondence for each character
Unicode code point。
utf-16And so on
Unicode code pointThe coding method of. such as
utf-8The number of bytes corresponding to the encoding is 1-4 bytes. Since the number of short bytes is less, it is given priority to the more frequently used characters（
Unicode code point)。
- JSON standard http://www.json.org.cn/standa…
- Character encoding notes: ASCII, Unicode and UTF-8 http://www.ruanyifeng.com/blo…
- Escape and Unicode encoding in JSON serialization https://segmentfault.com/a/11…
- Make Jason understand Chinese better (JSON)_ UNESCAPED_ UNICODE) https://www.laruence.com/2011…
- Online conversion of Chinese to UTF-8 encoding hexadecimal and Unicode encoding hexadecimal http://www.mytju.com/classcod…
- Online conversion of Chinese to unicode encoding hexadecimal https://utf8.supfree.net/
- Comparison of ASCII code table http://ascii.911cha.com/
- Linux od tool, check the binary format of the file https://wangchujiang.com/linu…
- Linux command learning summary: hexdump https://www.cnblogs.com/kerry…