baidu了一堆,发现基本都需要下载并调用第三方组件,嫌麻烦,最后找到这篇文章:https://www.cnblogs.com/index-html/p/js-str-to-gbk-ultra-lite.html
在他的基础上完善了该方法,调用方式:
decStr2 = GBKencodeURIComponent(decStr1);
decStr3 = GBKdecodeURIComponent(decStr2);
简单纯粹
<input type="button" value="GBK编解码测试" onclick="textDecodeZxy();" /><br>
原始值:<input type="text" id="iddecStr1" style="width:80%"/><br>
编码值:<input type="text" id="iddecStr2" style="width:80%"/><br>
解码值:<input type="text" id="iddecStr3" style="width:80%">
<script language='javascript'>
//GBK(GB2312)解码对照表
const tableEn = new Uint16Array(65536);
//GBK(GB2312)编码对照表
const tableEn2 = new Uint16Array(65536);
//Unicode字符|GBK(GB2312)和UTF-8对应关系
const rangesEn = [
[0xA1, 0xA9, 0xA1, 0xFE],
[0xB0, 0xF7, 0xA1, 0xFE],
[0x81, 0xA0, 0x40, 0xFE],
[0xAA, 0xFE, 0x40, 0xA0],
[0xA8, 0xA9, 0x40, 0xA0],
[0xAA, 0xAF, 0xA1, 0xFE],
[0xF8, 0xFE, 0xA1, 0xFE],
[0xA1, 0xA7, 0x40, 0xA0],
];
//UTF-8对照表
const codesEn = new Uint16Array(23940);
//编码初始化
function initEnCode() {
let i = 0;
for (const[b1Begin, b1End, b2Begin, b2End]of rangesEn) {
for (let b2 = b2Begin; b2 <= b2End; b2++) {
if (b2 !== 0x7F) {
for (let b1 = b1Begin; b1 <= b1End; b1++) {
codesEn[i++] = b2 << 8 | b1;
}
}
}
}
const str = new TextDecoder('gbk').decode(codesEn);
for (let i = 0; i < str.length; i++) {
tableEn[str.charCodeAt(i)] = codesEn[i];
tableEn2[codesEn[i]] = str.charCodeAt(i);
}
}
initEnCode();
/*初始全局变量*/
//GBK(GB2312)字节转字符串
function gbkToStr(buf){
var strResult = "";
var strIndex = 0;
for(let i = 0; i < buf.length; i++) {
if(buf[i] < 0x80){
strResult += String.fromCharCode(buf[i]);
strIndex ++;
}
else if(buf[i] >= 0x80 && buf[i + 1] >= 0x80){
var codeIndex = buf[i+1] << 8 | buf[i];
var code = tableEn2[codeIndex]
strResult += String.fromCharCode(code);
i ++;
strIndex += 2;
}
}
return strResult;
}
//字符串转GBK(GB2312)
function stringToGbk(str) {
initEnCode();
const buf = new Uint8Array(str.length * 2);
let n = 0;
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (code < 0x80) {
buf[n++] = code;
} else {
const gbk = tableEn[code];
buf[n++] = gbk & 0xFF;
buf[n++] = gbk >> 8;
}
}
return buf.subarray(0, n);
}
//GBK(GB2312)编码
function GBKencodeURIComponent(objTag){
var strResult = "";
var bytary = stringToGbk(objTag);
for(var i = 0; i < bytary.length; i ++){
if(bytary[i] < 0x80){
strResult += encodeURIComponent(String.fromCharCode(bytary[i]));
}
else{
strResult += "%"+bytary[i].toString(16).toUpperCase();
}
}
return strResult;
}
//GBK(GB2312)解码
function GBKdecodeURIComponent(objTag){
var strResult = "";
var strAry = Array.from(objTag);
for(var i = 0; i < strAry.length; i ++){
//判断是否中文编码字符
if(strAry[i] == "%" && strAry.length - i >= 6 && strAry[3 + i] == "%"){
var numByt = [];
//16进制
var Hex = strAry[i + 1] + strAry[i + 2];
//10进制
var num = parseInt(Hex, 16);
numByt[0] = num;
//ASCII
if(num < 0x80){
strResult += String.fromCharCode(num);
i += 2;
}
else{
//16进制
Hex = strAry[i + 4] + strAry[i + 5];
//10进制
num = parseInt(Hex, 16);
numByt[1] = num;
strResult += gbkToStr(numByt);
i += 5
}
}
//ASCII
else if(strAry[i] == "%" && strAry.length - i >= 6 && strAry[3 + i] != "%"){
var numByt = [];
//16进制
var Hex = strAry[i + 1] + strAry[i + 2];
//10进制
var num = parseInt(Hex, 16);
numByt[0] = num;
//ASCII
strResult += String.fromCharCode(num);
i += 2;
}
//ASCII
else if(strAry[i] == "%" && strAry.length - i < 6){
var numByt = [];
//16进制
var Hex = strAry[i + 1] + strAry[i + 2];
//10进制
var num = parseInt(Hex, 16);
numByt[0] = num;
//ASCII
strResult += String.fromCharCode(num);
i += 2;
}
else if(strAry[i] != "%"){
strResult += strAry[i];
}
}
return strResult;
}
function textDecodeZxy() {
/* var hexary = []; //16进制数组
var intAry = []; //10进制整数数组
var bytary = []; //字节数组
*/
var decStr1 = "";
var decStr2 = "";
var decStr3 = "";
/* bytary = stringToGbk('你好的说法认为yyyrr-fsdfsr123'); //[196, 227, 186, 195, 49, 50, 51]
var strInput = "%BF%AA%CA%BC%CA%B1%BC%E4%BB%F2%D5%DF%BD%E1"
+"%CA%F8%CA%B1%BC%E4%B8%F1%CA%BD%B4%ED%CE%F3%A3%AC%D3"
+"%A6%B8%C3%CE%AAyyyy-MM-dd";
decStr1 = GBKdecodeURIComponent(strInput);
*/
decStr1 = "测试数据`1234567890-=~!@#$%^&*()___+{}|:\"<>?[]\l;',.//'";
decStr2 = GBKencodeURIComponent(decStr1);
decStr3 = GBKdecodeURIComponent(decStr2);
document.getElementById("iddecStr1").value = decStr1;
document.getElementById("iddecStr2").value = decStr2;
document.getElementById("iddecStr3").value = decStr3;
}
</script>