- 手动解析 URL 编码(将 %xx 转换为字节);
- 构建字节数组;
- 使用 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)