Last active
July 21, 2018 21:55
-
-
Save iamhosseindhv/cb4b1832ba702d5fc1a430570133c96e to your computer and use it in GitHub Desktop.
Covert an input file to its binary representation
This file contains hidden or 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
| /** | |
| * Coverts the input file to its binary representation | |
| * Usage: node text2binary.js myBundle.js | |
| */ | |
| let fs = require('fs'); | |
| const inputFile = process.argv.slice(2)[0]; | |
| const outputPath = `${inputFile}_binary.txt`; | |
| fs.readFile(inputFile, 'utf8', (err, data) => { | |
| if (err) throw err; | |
| const noSpace = data.replace(/\s/g, ''); | |
| const file = JSON.stringify(noSpace, null, 2); | |
| const binarified = text2Binary(file); | |
| fs.writeFileSync(outputPath, binarified, 'utf-8'); | |
| }); | |
| const text2Binary = string => { | |
| return string.split('').map(char => { | |
| return char.charCodeAt(0).toString(2); | |
| }).join(' '); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment