js performs base64 encoding and decoding operations on Chinese to solve the problem of Chinese garbled characters

I use the interface of github api to get the content of the file, and then use atob to decode it, but I found: garbled characters....sorry

So there is the method I encapsulate:

export const encode64 = (str) => {
  // 首先,我们使用 encodeURIComponent 来获得百分比编码的UTF-8,然后我们将百分比编码转换为原始字节,最后存储到btoa里面
  return btoa(
    encodeURIComponent(str).replace(
      /%([0-9A-F]{2})/g,
      function toSolidBytes(_, p1) {
        return String.fromCharCode(Number("0x" + p1));
      }
    )
  );
};

export const decode64 = (str) => {
  // 过程:从字节流到百分比编码,再到原始字符串
  return decodeURIComponent(
    atob(str)
      .split("")
      .map(function (c) {
        return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
      })
      .join("")
  );
};

Then call:

Look at the result: perfect

Guess you like

Origin blog.csdn.net/weixin_44786530/article/details/132629882