Created
June 16, 2017 14:07
-
-
Save RodEsp/e492c00f5b29d7d9966465ac0818b7b9 to your computer and use it in GitHub Desktop.
Text-Binary translator
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
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