Fe-interview: [js] 第191天 举例说明atob和btoa的用法

Created on 23 Oct 2019  ·  4Comments  ·  Source: haizlin/fe-interview

第191天 举例说明atob和btoa的用法

js

Most helpful comment

是window的两个对象,btoa:binary to ascii;(base64的编码) atob:ascii to binary;(base64的解码) 无法用于Unicode字符

示例

```javascript
// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
```

All 4 comments

是window的两个对象,btoa:binary to ascii;(base64的编码) atob:ascii to binary;(base64的解码) 无法用于Unicode字符

示例

```javascript
// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
```

  • btoa: 字符串转base64编码
  • atob: base64编码转字符串

bug

image

将字符串转换成base64编码、或者解码。

btoa('hello') //"aGVsbG8="
atob('aGVsbG8') //"hello"

// 对于超过 Latin1 范畴的字符,可以使用 encodeURI/decodeURI 进行转换:
btoa(encodeURI('字')) // "JUU1JUFEJTk3"
decodeURI(atob('JUU1JUFEJTk3')) // "字"
Was this page helpful?
0 / 5 - 0 ratings