github APIのインターフェースを使ってファイルの内容を取得し、atobを使ってデコードしたのですが、文字化けが発生してしまいました。
そこで、私がカプセル化したメソッドがあります。
export const encode64 = (str) => {
// 首先,我们使用 encodeURIComponent 来获得百分比编码的UTF-8,然后我们将百分比编码转换为原始字节,最后存储到btoa里面
return btoa(
encodeURIComponent(str).replace(
/%([0-9A-F]{2})/g,
function toSolidBytes(_, p1) {
return String.fromCharCode(Number("0x" + p1));
}
)
);
};
export const decode64 = (str) => {
// 过程:从字节流到百分比编码,再到原始字符串
return decodeURIComponent(
atob(str)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
};
次に、次のように呼び出します。
結果を見てください: 完璧です