Skip to content

Instantly share code, notes, and snippets.

@birkir
Created November 14, 2014 10:54
Show Gist options
  • Save birkir/9b5540827b31b3f778be to your computer and use it in GitHub Desktop.
Save birkir/9b5540827b31b3f778be to your computer and use it in GitHub Desktop.
Convert 7-bit ASCII to Chuck Norris style binary
/**
* Convert 7-bit ASCII to Chuck Norris style binary
*
* @param string Characters to encode
* @return string Binary' norris style.
*/
function asciiToChuckNorris(chars) {
// Variables
var bits = [],
last;
// Encode ascii to bits
chars.split('').forEach(function (char, i) {
Array.apply(null, Array(7)).forEach(function (_, j) {
bits[i * 7 + j] = ((chars.charCodeAt(i) & (1 << (6 - j))) != 0) ? 1 : 0;
});
});
// Map bits norris style
return bits.map(function (bit) {
var ret = '';
if (last !== bit && bit === 1) ret += ' 0 ';
if (last !== bit && bit === 0) ret += ' 00 ';
last = bit;
return ret+'0';
}).join('').substr(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment