Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created Mar 21, 2017
Embed
What would you like to do?
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