繁体   English   中英

Jacascript base64 未根据需要解码

[英]Jacascript base64 not decode as needed

我的 html 代码中有这个字符串:

eyJzaW1wbGVUZXh0Ijoi8J+NjCBTVU1NRVIgU0VUIDIwMTkg8J+QnSBERSBMQSBLQVJJTkEg4pqhINeh15gg16fXmdelIDIwMTkg8J+MvSJ9

它在 Base64 中表示此代码:

{"simpleText":"🍌 SUMMER SET 2019 🐝 DE LA KARINA ⚡ סט קיץ 2019 🌽"}

使用 Base64 解码方法时遇到问题:

function decode(data) {
var value, code, idx = 0, bytes = [], leftbits = 0, leftdata = 0;
var binTable = [
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
];

var padding = '=';

for (idx = 0; idx < data.length; idx++) {
  code = data.charCodeAt(idx);
  value = binTable[code & 0x7F];

  if (-1 === value) {
    log("WARN: Illegal characters (code=" + code + ") in position " + idx);
  } else {
    leftdata = (leftdata << 6) | value;
    leftbits += 6;

    if (leftbits >= 8) {
      leftbits -= 8;
      if (padding !== data.charAt(idx)) {
        bytes.push((leftdata >> leftbits) & 0xFF);
      }
      leftdata &= (1 << leftbits) - 1;
    }
  }
}

if (leftbits) {
  log("ERROR: Corrupted base64 string");
  return null;
}

return utf8Decode(bytes);
}

这给了我这个字符串:

{"simpleText":"ߍ젓UMMER SET 2019 ߐ�E LA KARINA ⚡ סט קיץ 2019 ߌ�

我知道建议使用atob函数,但是因为它给我带来了其他问题,所以我更喜欢使用此代码,不知道为什么它不能在字符串中使用表情符号。

谢谢!

您可以使用带有转义字符串的 encodeURIComponent/decodeURIComponent 来取回您的表情符号。 (有关更多信息,请参见此处http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html

这是一个代码示例,显示了您的数据的编码/解码: https : //jsbin.com/wusokawacu/edit?js,console

const string = {"simpleText":"🍌 SUMMER SET 2019 🐝 DE LA KARINA ⚡ סט קיץ 2019 🌽"}


function utf8_to_b64(str) {
  return window.btoa(unescape(encodeURIComponent(str)));
}

function b64_to_utf8(str) {
  return decodeURIComponent(escape(window.atob(str)));
}

const encoded = utf8_to_b64(JSON.stringify(string));
const decoded = b64_to_utf8(encoded);

我觉得你的问题不是在decode()函数中,而是在里面使用的utf8Decode()函数中。

但是你没有提供它,所以我写了一个它为你的字符串提供了正确的结果(你可以在控制台中看到{"simpleText":"🍌 SUMMER SET 2019 🐝 DE LA KARINA ⚡ סט קיץ 2019 🌽"} ):

 const example = "eyJzaW1wbGVUZXh0Ijoi8J+NjCBTVU1NRVIgU0VUIDIwMTkg8J+QnSBERSBMQSBLQVJJTkEg4pqhINeh15gg16fXmdelIDIwMTkg8J+MvSJ9"; function decode(data) { var value, code, idx = 0, bytes = [], leftbits = 0, leftdata = 0; var binTable = [ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 ]; var padding = '='; for (idx = 0; idx < data.length; idx++) { code = data.charCodeAt(idx); value = binTable[code & 0x7F]; if (-1 === value) { console.log("WARN: Illegal characters (code=" + code + ") in position " + idx); } else { leftdata = (leftdata << 6) | value; leftbits += 6; if (leftbits >= 8) { leftbits -= 8; if (padding !== data.charAt(idx)) { bytes.push((leftdata >> leftbits) & 0xFF); } leftdata &= (1 << leftbits) - 1; } } } if (leftbits) { console.log("ERROR: Corrupted base64 string"); return null; } return utf8Decode(bytes); } function utf8Decode(bytes) { return new TextDecoder().decode(new Uint8Array(bytes)); } const result = decode(example); console.log(result);

我知道你说过你不想使用原生的 atob 函数

但是你有没有试过这样:

let encodedData = 'eyJzaW1wbGVUZXh0Ijoi8J+NjCBTVU1NRVIgU0VUIDIwMTkg8J+QnSBERSBMQSBLQVJJTkEg4pqhINeh15gg16fXmdelIDIwMTkg8J+MvSJ9';

let decodedData = decodeURIComponent(escape(window.atob(encodedData)));
console.log(decodedData); // will print your data with the emojis

如果你还想自己实现,看看这个包: https : //github.com/dankogai/js-base64/blob/master/base64.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM