为什么 '%20' 被用作 URL 中的空格的起源 [英] The origin on why '%20' is used as a space in URLs

查看:13
本文介绍了为什么 '%20' 被用作 URL 中的空格的起源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道为什么将%20"用作 URL 中的空格,尤其是为什么要使用 %20 以及为什么我们首先需要它.

I am interested in knowing why '%20' is used as a space in URLs, particularly why %20 was used and why we even need it in the first place.

推荐答案

它被称为百分比编码.某些字符不能在 URI 中(例如 #,因为它表示 URL 片段),因此它们用可以(# 变成 >%23)

It's called percent encoding. Some characters can't be in a URI (for example #, as it denotes the URL fragment), so they are represented with characters that can be (# becomes %23)

这是同一篇文章的摘录:

Here's an excerpt from that same article:

当保留集中的字符(保留字符")具有特定上下文中的特殊含义(保留目的")和 URI方案说有必要将该字符用于其他一些目的,那么字符必须是百分比编码.保留字符的百分比编码涉及转换字符转换为其对应的 ASCII 字节值,然后将该值表示为一对十六进制数字. 数字,前面是用作转义字符的百分号(%"),然后在 URI 中使用 代替保留字符.(为一个非 ASCII 字符,它通常在UTF-8,然后每个字节值如上表示.)

When a character from the reserved set (a "reserved character") has special meaning (a "reserved purpose") in a certain context, and a URI scheme says that it is necessary to use that character for some other purpose, then the character must be percent-encoded. Percent-encoding a reserved character involves converting the character to its corresponding byte value in ASCII and then representing that value as a pair of hexadecimal digits. The digits, preceded by a percent sign ("%") which is used as an escape character, are then used in the URI in place of the reserved character. (For a non-ASCII character, it is typically converted to its byte sequence in UTF-8, and then each byte value is represented as above.)

空格字符的字符代码为32:

The space character's character code is 32:

> ' '.charCodeAt(0)
32

base-16 中的 20 是什么:

Which is 20 in base-16:

> ' '.charCodeAt(0).toString(16)
"20"

在它前面加上一个百分号,你会得到 %20.

Tack a percent sign in front of it and you get %20.

这篇关于为什么 '%20' 被用作 URL 中的空格的起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆