Created
September 19, 2022 08:03
-
-
Save TheRolfFR/e1334545645fd6d516870bb8e1e96cdf to your computer and use it in GitHub Desktop.
JS String extension with codes and chars
This file contains 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
/** | |
* Gives char codes of the string | |
* @author TheRolf | |
*/ | |
String.prototype.codes = function() { | |
return this.split('').map(c => { | |
let code = c.charCodeAt(0); | |
return code; | |
}).flat() | |
} | |
/** | |
* Gives chars of characters of the string, big-endian | |
* @author TheRolf | |
*/ | |
String.prototype.chars = function() { | |
return this.split('').map(c => { | |
let code = c.charCodeAt(0); | |
let arr = []; | |
while(code > 0) { | |
let b = code & 255; | |
arr = [b, ...arr]; | |
code = code >> 8; | |
} | |
return arr; | |
}).flat() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment