Last active
November 9, 2016 14:42
-
-
Save liamnewmarch/3b96b287d95c83e5e26459b860e34e99 to your computer and use it in GitHub Desktop.
Convert ascii text to and from binary.
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
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