jschain: converting strings to and from binary (#14)

Encoding to binary:

'hello'.replace(/./g, function(c) {
function pad(n) { return (n > 0) ? ('0' + pad(n - 1)) : ''; }
var s = c.charCodeAt(0).toString(2);
return pad(8 - s.length) + s;
});

Decoding binary to a string:

'011101110110111101110111'.replace(/.{8}/g, function(s) {
return String.fromCharCode(parseInt(s, 2));
});

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.