Js btoa方法
时间: 2023-08-05 14:02:46 浏览: 70
在 JavaScript 中,`btoa()` 方法用于将字符串编码为 base-64 格式。这个方法接受一个字符串作为参数,返回一个编码后的字符串。下面是一个使用 `btoa()` 方法的示例:
```javascript
var str = "Hello, world!";
var encodedStr = btoa(str);
console.log(encodedStr); // 输出 "SGVsbG8sIHdvcmxkIQ=="
```
注意,`btoa()` 方法只能编码 ASCII 字符,如果要编码非 ASCII 字符,需要先将其转换为 UTF-8 格式。而且,由于 base-64 格式编码后的字符串长度会比原始字符串长度增加约 33%,因此对于大量数据的编码,需要额外考虑传输效率的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)