Skip to content

Instantly share code, notes, and snippets.

@RodEsp
Created June 16, 2017 14:07
Show Gist options
  • Save RodEsp/e492c00f5b29d7d9966465ac0818b7b9 to your computer and use it in GitHub Desktop.
Save RodEsp/e492c00f5b29d7d9966465ac0818b7b9 to your computer and use it in GitHub Desktop.
Text-Binary translator
const convertTextToBinary = (text) => {
const chars = text.split('');
return chars.reduce((str, byte, i) => {
byte = byte.charCodeAt(0).toString(2);
byte = new Array(9 - byte.length).join(0) + byte;
return str + byte + (i === chars.length - 1 ? '' : ' ');
}, '');
}
const convertBinaryToText = (binCode) => {
const bytes = binCode.split(' ');
return bytes.reduce((str, byte) => {
return str + String.fromCharCode(parseInt(byte, 2));
}, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment