📜  nodejs btoa - Javascript (1)

📅  最后修改于: 2023-12-03 14:44:43.696000             🧑  作者: Mango

Node.js btoa - JavaScript

In JavaScript, btoa() function is used to encode a string in base-64. In Node.js, this function is available as a global object method btoa().

Syntax
btoa(str)
Parameters
  • str - A string to be encoded in base-64.
Return Value
  • A string that represents the base-64 encoded version of the input string.
Example
const str = "Hello, world!";
const encoded = btoa(str);
console.log(encoded);
// SGVsbG8sIHdvcmxkIQ==
Supported Characters

btoa() function can only encode the characters that belong to the 7-bit ASCII character set.

If the input string contains any characters beyond this set, it will throw a DOMException.

Therefore, if you need to encode text containing characters beyond the ASCII set, you should use a library that supports Unicode (UTF-8) encoding, such as utf8.

Base-64 Encoding

Base-64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation.

Base-64 encoding is used in numerous applications, including email and security protocols. It is also commonly used to encode binary data that needs to be transmitted over channels that are designed to handle only textual data, such as HTTP requests and responses.

The encoded text is made up entirely of 64 unique characters, which makes it easy to transmit over a variety of systems that use different protocols. The base-64 encoded string can also be easily converted back to its original form.