在Javascript中,window.atob()
method解码base64字符串,然后window.btoa()
将a编码string
为base64。
那么,为什么没有他们命名为喜欢的base64Decode()
和base64Encode()
?
atob()
并且btoa()
没有任何意义,因为它们根本不是语义的。
我想知道原因。
name
,平时他们用清长的名字一样getElementById
,addEventListener
在Javascript中,window.atob()
method解码base64字符串,然后window.btoa()
将a编码string
为base64。
那么,为什么没有他们命名为喜欢的base64Decode()
和base64Encode()
?
atob()
并且btoa()
没有任何意义,因为它们根本不是语义的。
我想知道原因。
name
,平时他们用清长的名字一样getElementById
,addEventListener
Answers:
该atob()
和btoa()
方法允许作者进行改造的内容,并从base64编码。
在这些API中,出于记忆的目的,可以将“ b”视为“二进制”,将“ a”视为“ ASCII”。但是实际上,由于主要的历史原因,这些函数的输入和输出都是Unicode字符串。
atob()
将二进制转换为ASCII,btoa()
并将ASCII转换为二进制。
atob
是二进制的ascii。他们有点忽略了这两个答案。因此它不会逆转
String
是Binary
?!而且我一直都以为,二进制的值大约是0和1。
window.btoa
将其参数读取为二进制数据,并将其拆分为6位块以对其进行编码;的确如此,因此从某种角度来说命名是有意义的。但是,也window.btoa
只能将字符串作为参数!:(
window.btoa
只将字符串作为参数!” <是的,但是这里的字符串仅代表数据。就像您尝试在记事本中打开图像一样,它会显示为字符串,但仍然是二进制数据。btoa
的主要优点是它并不关心字符串的格式,它只是将其视为二进制。在大多数情况下,该字符串碰巧是常规字符串,这只是偶然。
总结已经给出的答案:
atob
代表 ASCII to binary
atob("ZXhhbXBsZSELCg==") == "example!^K"
btoa
代表 binary to ASCII
btoa("\x01\x02\xfe\xff") == "AQL+/w=="
为什么一个 SCII和b inary:
ASCII
(a
)是base64
编码的结果。仅由可以正确表示和传输的ascii字符(*)子集组成的安全文本(例如电子邮件的正文),binary
(b
)是0和1的任何流(在javascript中,它必须以字符串类型表示)。(*)在base64
这些被限制为:A-Z
,a-z
,0-9
,+
,/
和=
(填充,仅在端部)https://en.wikipedia.org/wiki/Base64
PS我必须承认,我本人最初对命名感到困惑,并认为名称已被交换。我认为b
立场“ b ase64编码字符串”,并a
为“ 一个纽约字符串”:d。
btoa
技术上的输出仍然是ASCII,但没有理由atob
只接受base64作为输入的名称。
我知道这很旧,但是最近在Twitter上出现了,我认为我会分享它,因为它是权威性的。
我:
@BrendanEich您选择了那些名字吗?
他:
旧的Unix名称,很难找到手册页rn,但请参见 https://www.unix.com/man-page/minix/1/btoa/。名称从Unix延续到Netscape代码库中。我在1995年匆忙地将它们反映到JS中(在5月的十天之后,但很快)。
万一Minix链接中断,以下是手册页内容:
BTOA(1) BTOA(1)
NAME
btoa - binary to ascii conversion
SYNOPSIS
btoa [-adhor] [infile] [outfile]
OPTIONS
-a Decode, rather than encode, the file
-d Extracts repair file from diagnosis file
-h Help menu is displayed giving the options
-o The obsolete algorithm is used for backward compatibility
-r Repair a damaged file
EXAMPLES
btoa <a.out >a.btoa # Convert a.out to ASCII
btoa -a <a.btoa >a.out
# Reverse the above
DESCRIPTION
Btoa is a filter that converts a binary file to ascii for transmission over a telephone
line. If two file names are provided, the first in used for input and the second for out-
put. If only one is provided, it is used as the input file. The program is a function-
ally similar alternative to uue/uud, but the encoding is completely different. Since both
of these are widely used, both have been provided with MINIX. The file is expanded about
25 percent in the process.
SEE ALSO
uue(1), uud(1).
资料来源:JavaScript的创建者Brendan Eich。https://twitter.com/BrendanEich/status/998618208725684224