admin管理员组文章数量:1128454
In Javascript, window.atob()
method decodes a base64 string and window.btoa()
method encodes a string
into base64.
Then why weren't they named like base64Decode()
and base64Encode()
?
atob()
and btoa()
don't make sense because they're not semantic at all.
I want to know the reason.
In Javascript, window.atob()
method decodes a base64 string and window.btoa()
method encodes a string
into base64.
Then why weren't they named like base64Decode()
and base64Encode()
?
atob()
and btoa()
don't make sense because they're not semantic at all.
I want to know the reason.
Share Improve this question edited Oct 6, 2018 at 8:54 Константин Ван asked Nov 22, 2015 at 11:11 Константин ВанКонстантин Ван 14.7k8 gold badges72 silver badges86 bronze badges 5 |5 Answers
Reset to default 274I asked Brendan Eich (the creator of JavaScript) if he picked those names on Twitter and he responded:
Old Unix names, hard to find man pages rn but see https://www.unix.com/man-page/minix/1/btoa/ …. The names carried over from Unix into the Netscape codebase. I reflected them into JS in a big hurry in 1995 (after the ten days in May but soon).
In case the Minix link breaks, here's the man page content:
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).
The atob()
and btoa()
methods allow authors to transform content to and from the base64 encoding.
In these APIs, for mnemonic purposes, the "b" can be considered to stand for "binary", and the "a" for "ASCII". In practice, though, for primarily historical reasons, both the input and output of these functions are Unicode strings.
From : http://www.w3.org/TR/html/webappapis.html#atob
To sum up the already given answers:
atob
stands forASCII to binary
- e.g.:
atob("ZXhhbXBsZSELCg==") == "example!^K"
- e.g.:
btoa
stands forbinary to ASCII
- e.g.:
btoa("\x01\x02\xfe\xff") == "AQL+/w=="
- e.g.:
Why ASCII and binary:
ASCII
(thea
) is the result ofbase64
encoding. A safe text composed only of a subset of ascii characters(*) that can be correctly represented and transported (e.g. email's body),binary
(theb
) is any stream of 0s and 1s (in javascript it must be represented with a string type).
(*) in base64
these are limited to: A-Z
, a-z
, 0-9
, +
, /
and =
(padding, only at the end) https://en.wikipedia.org/wiki/Base64
P.S. I must admit I myself was initially confused by the naming and thought the names were swapped. I thought that b
stand for "base64 encoded string" and a
for "any string" :D.
The names come from a unix function with similar functionality, but you can already read that in other answers here.
Here is my mnemonic to remember which one to use. This doesn't really answer the question itself, but might help people figure which one of the functions to use without keeping a tab open on this question on stack overflow all day long.
Beautiful to Awful btoa
Take something Beautiful (aka, beautiful content that would make sense to your application: json, xml, text, binary data) and transform it to something Awful, that cannot be understood as is (aka: encoded).
Awful to Beautiful atob
The exact opposite of btoa
Note
Some may say that binary is not beautiful, but hey, this is only a trick to help you.
I can't locate a source at the moment, but it is common knowledge that in this case, the b stands for 'binary', and the a for 'ASCII'.
Therefore, the functions are actually named:
ASCII to Binary for atob()
, and
Binary to ASCII for btoa()
.
Alos, note that this is browser implementation and was left for legacy / backward compatibility purposes. In Node.js, you would use:
Buffer.from("Hello World").toString('base64')
Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii')
本文标签: Why were Javascript atob() and btoa() named like thatStack Overflow
版权声明:本文标题:Why were Javascript `atob()` and `btoa()` named like that? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736717063a1949257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
atob
stands for binary, not base64. ASCII is base64 encoded, and strings are binary. – Janac Meena Commented Jun 27, 2021 at 18:00