Last active
May 9, 2024 05:11
-
-
Save littlee/f726f61b1e0abd319da4 to your computer and use it in GitHub Desktop.
JavaScript convert string to unicode format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toUnicode(str) { | |
return str.split('').map(function (value, index, array) { | |
var temp = value.charCodeAt(0).toString(16).toUpperCase(); | |
if (temp.length > 2) { | |
return '\\u' + temp; | |
} | |
return value; | |
}).join(''); | |
} | |
var str = '转换成 Unicode'; | |
console.log(toUnicode(str)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mm