JavaScript 中文转GBK编码长度计算方法
function chineseToGBK(str) { let len = 0; for (let i = 0; i < str.length; i++) { const code = str.charCodeAt(i); if (code >= 0x4E00 && code <= 0x9FA5) { len += 2; } else { len += 1; } } return len; }
console.log(chineseToGBK('中文')); // 4
原文地址: https://www.cveoy.top/t/topic/oLlY 著作权归作者所有。请勿转载和采集!