首页 > 其他分享 >前端中文转base64

前端中文转base64

时间:2023-03-09 17:56:42浏览次数:27  
标签:编码 中文 TextEncoder 前端 base64 btoa atob

btoa

浏览器的方法,可将字符串编码为 Base64 编码的 ASCII 字符串。

btoa("hello world"); // 'aGVsbG8gd29ybGQ='

UTF-16 字符编码表示不了中文,可以先把中文编码为Uint8Array


// 1. 使用TextEncoder 编码
let textEncoder = new TextEncoder();

let encoded = textEncoder.encode("你好!");
btoa(encoded); // 'MjI4LDE4OSwxNjAsMjI5LDE2NSwxODksMjM5LDE4OCwxMjk='

// 2. 使用encodeURIComponent
btoa(encodeURIComponent("你好!"));

atob

对编码的base64进行解码。

atob('aGVsbG8gd29ybGQ='); // hello world

标签:编码,中文,TextEncoder,前端,base64,btoa,atob
From: https://www.cnblogs.com/dreamHot/p/17199268.html

相关文章