转载: https://blog.csdn.net/guxiaonuan/article/details/78678043
// 附: 十进制数值转 utf-8 编码方法, 如果要从 Unicode 编码转 utf-8, 可通过 parseInt(unicode, 16) 得到 unicode 编码的十进制表示再转
function decimal2Utf8(num) { if (num < 0 || num > Math.pow(2, 31) - 1) { throw new Error('RangeError: param num is expect 0 ~ 2^31-1, but received ' + num) } var strs = [], start = 0, end, digit, binaryStr, biteLen, biteLen2, strLen, cur digit = Math.log2(num); binaryStr = num.toString(2); strLen = binaryStr.length if (digit < 7) { strs.push(binaryStr.padStart(8, '0')); } else { biteLen = (digit - 3) % 4 === 0 ? (digit - 3) / 4 + 1 : Math.ceil((digit - 3) / 4); biteLen2 = biteLen; while(biteLen) { end = strLen - (biteLen - 1) * 6; // 如果是最高位 if (biteLen === biteLen2) { cur = ''.padStart(biteLen, 1) + binaryStr.substring(start, end).padStart(8 - biteLen, '0') } else { cur = '1' + binaryStr.substring(start, end).padStart(7, '0') } strs.push(cur) start = end; biteLen--; } } for (var i = 0; i < strs.length; i++) { strs[i] = '%' + parseInt(strs[i], 2).toString(16).toUpperCase() } return strs.join('') }
附:
https://unicode-table.com/cn/blocks/
http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
https://baike.baidu.com/item/Unicode/750500?fr=aladdin