Skip to content

Instantly share code, notes, and snippets.

@Oaphi
Last active December 6, 2020 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oaphi/2bc4f26f2be6879f2d87ff61df1a84a6 to your computer and use it in GitHub Desktop.
Save Oaphi/2bc4f26f2be6879f2d87ff61df1a84a6 to your computer and use it in GitHub Desktop.
Convert Unicode literal to hexadecimal byte sequence
/**
* @summary converts unicode literal \u{<code point>} into hex byte sequence \x<byte>...
*/
const fromUnicodeToHexBytes = (unicode: string) => {
//make bytes unsigned -128 +127 -> +256 or & 0xff -> 0 256
return Utilities.newBlob(unicode)
.getBytes()
.map((bt) => `\\x${(bt & 0xff).toString(16)}`)
.join("");
};
/**
* @summary same thing using built-in function
*/
const toHexBytes = (unicode: string) => encodeURIComponent(unicode).replace(/%/g, "\\x");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment