xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

fix btoa decoded error All In One

fix btoa decoded error All In One

base64 encode / decode

Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded contains characters outside of the Latin1 range.

Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

error

let s = `此密码只会在此设备上解锁您的 MetaMask 钱包。MetaMask 无法恢复此密码。`;

atob(s)
// Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded contains characters outside of the Latin1 range.

btoa(s)
// Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

solution

btoa(unescape(encodeURIComponent(s)));
// '5q2k5a+G56CB5Y+q5Lya5Zyo5q2k6K6+5aSH5LiK6Kej6ZSB5oKo55qEIE1ldGFNYXNrIOmSseWMheOAgk1ldGFNYXNrIOaXoOazleaBouWkjeatpOWvhueggeOAgg=='

atob(unescape(encodeURIComponent(s)));
// Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

image

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

demos

function encodeBase64(str = '') {
  // string 转成 base64
   return window.btoa(unescape(encodeURIComponent(str)));
}

function decodeBase64(str = '') {
   // base64  反转 string (逆序嵌套)
   return decodeURIComponent(escape(window.atob(str)));
}


const text = 'Vue3 script setup pass props';

const binary = encodeBase64(text);
console.log(`binary =`, binary);

const string = decodeBase64(binary);
console.log(`string =`, string);

image

btoa()

The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).

https://developer.mozilla.org/en-US/docs/Web/API/btoa

atob()

The atob() function decodes a string of data which has been encoded using Base64 encoding. You can use the btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again. For example, you can encode, transmit, and decode control characters such as ASCII values 0 through 31.

https://developer.mozilla.org/en-US/docs/Web/API/atob

refs

https://stackoverflow.com/questions/23223718/failed-to-execute-btoa-on-window-the-string-to-be-encoded-contains-characte

window.atob lost spaces bug All In One

转换成 Base64 字符串后,丢失空格 ❌

https://www.cnblogs.com/xgqfrms/p/16034913.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-03-23 13:56  xgqfrms  阅读(895)  评论(1编辑  收藏  举报