1、代码示例
let str = 'hello china'
let temp = []
for(let i = 0; i < str.length; i++){
let char = str.charCodeAt(i)
//转换为16进制unicode
console.log('\\u' + char.toString(16))
temp.push(char)
}
temp.forEach(function (item){
//String.fromCharCode()参数接收的是10进制,多个参数间可以用逗号分隔
let str = String.fromCharCode(item)
//替代console.log()可以不换行
process.stdout.write(str)
})
该代码示例展示了如何将字符串转换为16进制Unicode,并使用JavaScript的String.fromCharCode()函数将16进制值还原为字符。通过console.log()和process.stdout.write()分别进行打印,适用于理解Unicode编码和字符输出。
1775

被折叠的 条评论
为什么被折叠?



