1. 手动解析 URL 编码(将 %xx 转换为字节);
  2. 构建字节数组;
  3. 使用 TextDecoder(‘gbk’) 解码。
function decodeGB2312FromURL(encodedStr: string) {
  const bytes = [];
  for (let i = 0; i < encodedStr.length; i++) {
    if (encodedStr[i] === '%') {
      const hex = encodedStr[i + 1] + encodedStr[i + 2];
      bytes.push(parseInt(hex, 16));
      i += 2;
    } else {
      bytes.push(encodedStr.charCodeAt(i));
    }
  }
  const byteArray = new Uint8Array(bytes);
  const decoder = new TextDecoder('gbk');
  return decoder.decode(byteArray);
}
Code language: JavaScript (javascript)

0
希望看到您的想法,请您发表评论x