Skip to content

Instantly share code, notes, and snippets.

@lucymtc
Created July 30, 2018 10:27
Show Gist options
  • Select an option

  • Save lucymtc/0f396a8749d448fea28c00b51d25c24b to your computer and use it in GitHub Desktop.

Select an option

Save lucymtc/0f396a8749d448fea28c00b51d25c24b to your computer and use it in GitHub Desktop.
JavaScript - String to Unicode
const toUnicode = (str) => {
let unicodeString = '';
for (var i = 0; i < str.length; i ++) {
let unicodeStr = str.charCodeAt(i).toString(16).toUpperCase();
while (unicodeStr.length < 4) {
unicodeStr = `0${unicodeStr}`;
}
unicodeStr = `\\u${unicodeStr}`;
unicodeString += unicodeStr;
}
return unicodeString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment