Javascript 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 函数,但因为它给我带来了其他问题,所以我更喜欢使用这段代码,知道为什么它不能在字符串中使用表情符号。

谢谢!

我知道你说不想用原生的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

您可以使用带转义字符串的 encodeURIComponent/decodeURIComponent 取回您的表情符号。 (查看此处了解更多信息 http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html

这是显示 encoding/decoding 和您的数据的代码示例: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);