unicode 转 utf16

 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;

 

posted @ 2023-06-22 12:32  万物有序  阅读(18)  评论(0编辑  收藏  举报