Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created March 21, 2017 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukem512/8e325c287b8ad720584f39d729cf2d33 to your computer and use it in GitHub Desktop.
Save lukem512/8e325c287b8ad720584f39d729cf2d33 to your computer and use it in GitHub Desktop.
Dense Binary Text Encoding
// Dense Binary Text Encoding
// Encode each of the 26 letter as 1 to 26, in binary.
// Encode spaces as 0.
const ASCII_A = 65;
const ENCODED_SPACE = 0;
function dbte(str) {
const digits = str.toUpperCase().split('');
const encoding = digits.reduce((obj, d, i) => {
obj.push(d === ' ' ? ENCODED_SPACE : d.charCodeAt(0) - ASCII_A);
return obj;
}, []);
return encoding.map(d => {
const bin = d.toString(2);
return '0'.repeat(5 - bin.length) + bin;
});
}
const encoded = dbte('fidelity to purpose').join(' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment