首页 > 解决方案 > Jacascript base64 未根据需要解码

问题描述

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

谢谢!

标签: javascripthtmlbase64

解决方案


您可以使用带有转义字符串的 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);

推荐阅读