Skip to content

Instantly share code, notes, and snippets.

@shhider
Forked from littlee/toUnicode.js
Last active July 29, 2021 08:52
Show Gist options
  • Save shhider/8c19ede4cca3aa70729e2d0fe03fb6b8 to your computer and use it in GitHub Desktop.
Save shhider/8c19ede4cca3aa70729e2d0fe03fb6b8 to your computer and use it in GitHub Desktop.
[JavaScript convert string to unicode format] #tounicode
const toUnicode = (str) => str.split('').map((char) => {
const temp = char.charCodeAt(0).toString(16).toUpperCase();
if (temp.length > 2) {
return '\\u' + temp;
}
return char;
}).join('');
console.log(toUnicode('转换成 Unicode'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment