Skip to content

Instantly share code, notes, and snippets.

@TheRolfFR
Created September 19, 2022 08:03
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 TheRolfFR/e1334545645fd6d516870bb8e1e96cdf to your computer and use it in GitHub Desktop.
Save TheRolfFR/e1334545645fd6d516870bb8e1e96cdf to your computer and use it in GitHub Desktop.
JS String extension with codes and chars
/**
* 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