function gbkEncode(str) { let len = 0; for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) < 128) { len++; } else { len += 2; } } return { 'encoded': encodeURIComponent(str), 'length': len }; }

// 示例 const str = '中文GB2312编码'; const result = gbkEncode(str); console.log(result.encoded); // '%D6%D0%CE%C4GB2312%BC%FE%B1%A8' console.log(result.length); // 20

JavaScript 函数:将中文转换为GBK编码并获取长度

原文地址: https://www.cveoy.top/t/topic/oLl6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录