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;
标签:10,const,16,text,rest,toString,unicode,utf16 From: https://www.cnblogs.com/aurora-power/p/17497662.html