Last active
December 6, 2020 05:54
-
-
Save Oaphi/2bc4f26f2be6879f2d87ff61df1a84a6 to your computer and use it in GitHub Desktop.
Convert Unicode literal to hexadecimal byte sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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