Skip to content

Instantly share code, notes, and snippets.

@iamhosseindhv
Last active July 21, 2018 21:55
Show Gist options
  • Select an option

  • Save iamhosseindhv/cb4b1832ba702d5fc1a430570133c96e to your computer and use it in GitHub Desktop.

Select an option

Save iamhosseindhv/cb4b1832ba702d5fc1a430570133c96e to your computer and use it in GitHub Desktop.
Covert an input file to its binary representation
/**
* 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