Skip to content

Instantly share code, notes, and snippets.

@littlee
Last active May 9, 2024 05:11
Show Gist options
  • Save littlee/f726f61b1e0abd319da4 to your computer and use it in GitHub Desktop.
Save littlee/f726f61b1e0abd319da4 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));
@Sjshskdhd
Copy link

Mm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment