Created
November 14, 2014 10:54
-
-
Save birkir/9b5540827b31b3f778be to your computer and use it in GitHub Desktop.
Convert 7-bit ASCII to Chuck Norris style binary
This file contains 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
/** | |
* 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