Skip to content

Instantly share code, notes, and snippets.

@liamnewmarch
Last active November 9, 2016 14:42
Show Gist options
  • Save liamnewmarch/3b96b287d95c83e5e26459b860e34e99 to your computer and use it in GitHub Desktop.
Save liamnewmarch/3b96b287d95c83e5e26459b860e34e99 to your computer and use it in GitHub Desktop.
Convert ascii text to and from binary.
class BinaryText {
static encode(string) {
return string.split('').map(char => {
const charCode = char.charCodeAt(0);
return charCode.toString(2);
}).join('');
}
static decode(binary) {
return binary.match(/.{8}/g).map(byte => {
const charCode = parseInt(byte, 2);
return String.fromCharCode(charCode);
}).join('');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment