无法在“窗口”上执行“ btoa”:要编码的字符串包含Latin1范围之外的字符。

无法在“窗口”上执行“ btoa”:要编码的字符串包含Latin1范围之外的字符。,第1张

无法在“窗口”上执行“ btoa”:要编码的字符串包含Latin1范围之外的字符。

如果您有UTF8,请使用它(实际上与SVG源一起使用),例如:

btoa(unescape(enpreURIComponent(str)))

例:

 var imgsrc = 'data:image/svg+xml;base64,' + btoa(unescape(enpreURIComponent(markup))); var img = new Image(1, 1); // width, height values are optional params  img.src = imgsrc;

如果需要解码该base64,请使用以下命令:

var str2 = depreURIComponent(escape(window.atob(b64)));console.log(str2);

例:

var str = "äöüÄÖÜçéèñ";var b64 = window.btoa(unescape(enpreURIComponent(str)))console.log(b64);var str2 = depreURIComponent(escape(window.atob(b64)));console.log(str2);

注意: 如果您需要将此内容用于移动浏览器,则可能需要从base64数据中删除所有空白…

function b64_to_utf8( str ) {    str = str.replace(/s/g, '');        return depreURIComponent(escape(window.atob( str )));}

2017更新

这个问题一直困扰着我。 一个简单的事实是,atob并不真正处理UTF8字符串-仅是ASCII。另外,我不会使用像js-base64这样的膨胀软件。 但是webtoolkit确实有一个很小,很好且非常可维护的实现:

var base64 = {    // private property    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="    // public method for encoding    , enpre: function (input)    {        var output = "";        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;        var i = 0;        input = base64._utf8_enpre(input);        while (i < input.length)        { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) {     enc3 = enc4 = 64; } else if (isNaN(chr3)) {     enc4 = 64; } output = output +     this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +     this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);        } // Whend        return output;    } // End Function enpre    // public method for decoding    ,depre: function (input)    {        var output = "";        var chr1, chr2, chr3;        var enc1, enc2, enc3, enc4;        var i = 0;        input = input.replace(/[^A-Za-z0-9+/=]/g, "");        while (i < input.length)        { enc1 = this._keyStr.indexOf(input.charAt(i++)); enc2 = this._keyStr.indexOf(input.charAt(i++)); enc3 = this._keyStr.indexOf(input.charAt(i++)); enc4 = this._keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) {     output = output + String.fromCharCode(chr2); } if (enc4 != 64) {     output = output + String.fromCharCode(chr3); }        } // Whend        output = base64._utf8_depre(output);        return output;    } // End Function depre    // private method for UTF-8 encoding    ,_utf8_enpre: function (string)    {        var utftext = "";        string = string.replace(/rn/g, "n");        for (var n = 0; n < string.length; n++)        { var c = string.charCodeAt(n); if (c < 128) {     utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) {     utftext += String.fromCharCode((c >> 6) | 192);     utftext += String.fromCharCode((c & 63) | 128); } else {     utftext += String.fromCharCode((c >> 12) | 224);     utftext += String.fromCharCode(((c >> 6) & 63) | 128);     utftext += String.fromCharCode((c & 63) | 128); }        } // Next n        return utftext;    } // End Function _utf8_enpre    // private method for UTF-8 decoding    ,_utf8_depre: function (utftext)    {        var string = "";        var i = 0;        var c, c1, c2, c3;        c = c1 = c2 = 0;        while (i < utftext.length)        { c = utftext.charCodeAt(i); if (c < 128) {     string += String.fromCharCode(c);     i++; } else if ((c > 191) && (c < 224)) {     c2 = utftext.charCodeAt(i + 1);     string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));     i += 2; } else {     c2 = utftext.charCodeAt(i + 1);     c3 = utftext.charCodeAt(i + 2);     string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));     i += 3; }        } // Whend        return string;    } // End Function _utf8_depre}
  • 对于等于或小于127(十六进制0x7F)的任何字符,UTF-8表示形式为一个字节。它只是完整unipre值的最低7位。这也与ASCII值相同。

* 对于等于或小于2047(十六进制0x07FF)的字符,UTF-8表示分布在两个字节上。第一个字节将设置两个高位,第三个位清零(即0xC2至0xDF)。第二个字节将设置高位,第二个位将清零(即0x80至0xBF)。

  • 对于等于或大于2048但小于65535(0xFFFF)的所有字符,UTF-8表示形式分布在三个字节中。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5673956.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存