Skip to content

Instantly share code, notes, and snippets.

@Royalone94
Forked from littlee/toUnicode.js
Created July 11, 2019 13:01
Show Gist options
  • Save Royalone94/90a632851acfbafab98020cd9709513c to your computer and use it in GitHub Desktop.
Save Royalone94/90a632851acfbafab98020cd9709513c to your computer and use it in GitHub Desktop.
JavaScript convert string to unicode format
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