Created
March 21, 2017 23:42
-
-
Save lukem512/8e325c287b8ad720584f39d729cf2d33 to your computer and use it in GitHub Desktop.
Dense Binary Text Encoding
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
// 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