1 function toUtf16(text) {
2 if (text.length === 1) return text.charCodeAt(0).toString(16);
3 const point = text.codePointAt(0);
4 const highBits = 0xd800,
5 lowBits = 0xdc00,
6 bmp = 0x10000;
7 const rest = (point - bmp).toString(2),
8 restHighBits = +("0b" + rest.slice(0, -10)),
9 restLowBits = +("0b" + rest.slice(-10));
10 const _char0Hex = (highBits + restHighBits).toString(16),
11 _char1Hex = (lowBits + restLowBits).toString(16);
12
13 return _char0Hex + _char1Hex;
14 }
15
16 export default toUtf16;
unicode 转 utf16
最新推荐文章于 2024-08-03 20:32:29 发布
该代码定义了一个名为toUtf16的函数,用于将文本转换为UTF-16编码。它首先检查单字符情况,然后利用codePointAt方法获取Unicode码点,通过计算高位和低位字节来构造UTF-16编码的16进制表示。
摘要由CSDN通过智能技术生成