Home > Article > Backend Development > Each %XX after urlencode represents a byte?
After Chinese urlencode, each %XX represents a byte, right?
So the result of urlencode('中') is %XX%XX%XX (utf-8 encoding)
After Chinese urlencode, each %XX represents a byte, right?
So the result of urlencode('中') is %XX%XX%XX (utf-8 encoding)
Yes, in order to solve the problem of unicode taking up too much memory space and unchanged expansion, the utf-8 specification appeared.
For single-byte symbols, the first bit of the byte is set to 0, and the next 7 bits are the Unicode code of this symbol. So for English letters, UTF-8 encoding and ASCII code are the same.
For n-byte symbols (n>1), the first n bits of the first byte are set to 1, the n+1th bit is set to 0, and the first two bits of the following bytes are set to 10. The remaining binary bits not mentioned are all the unicode code of this symbol.
That is to say, the encoding result of UTF-8 is variable length. The utf-8 encoding of the
character in is
E4B8AD
, so the corresponding urlencode is %E4%B8%AD
.
Yes, URL encoding simply represents the data of special symbols and non-ASCII characters in hexadecimal, and then adds a percent sign before each byte (that is, two hexadecimal numbers). For those non-special ASCII characters the URL encoding is itself.
The same Chinese character, if it is GBK encoding, it will be two bytes, if it is UTF-8, it will be three bytes.
Of course, the more troublesome thing is that the URL encoding is confusing. For example, in some places, +
is used to represent spaces, and in others, %20
is used. The specific situation still needs to be analyzed in detail. The former corresponds to the function urlencode
, and the latter corresponds to rawurlencode
. Usually the former is used in form data (including the query in the URL, which is the part after ?
), and the latter is used in the URL path (the part after host and before query)