-
-
Save Royalone94/1af40032ea24f34d34874fdaf80b7ca5 to your computer and use it in GitHub Desktop.
JavaScript convert string to unicode format
This file contains 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