Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Last active August 29, 2015 14:20
Show Gist options
  • Save JavaScript-Packer/ba72bd8309fad4da7af6 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/ba72bd8309fad4da7af6 to your computer and use it in GitHub Desktop.
Binary encoder/decoder functions that let you choose a combiner/joiner for each binary. Live demo on http://www.whak.ca/demo/encode-decode-binary.htm
function ascii2binary(e, joiner) {
return Array.prototype.map.call(e, function(e) {
var t = e.charCodeAt(0).toString(2);
return "00000000".substr(t.length) + t;
}).join(joiner);
}
function binary2ascii(e, joiner) {
if (!joiner)joiner=" "; //added so you can leave joiner blank e.g. joiner="";
return e.replace(RegExp(joiner + "*[01]{8}" + joiner + "*", "g"), function(e) {
return String.fromCharCode(parseInt(e, 2));
});
}
var txt = "www.WHAK.com", joiner = "\n", encode = ascii2binary(txt, joiner), decode = binary2ascii(encode, joiner);
alert(encode + " = " + decode);
@JavaScript-Packer
Copy link
Author

Minified version:

function r(r,n){return Array.prototype.map.call(r,function(r){var n=r.charCodeAt(0).toString(2);return"00000000".substr(n.length)+n}).join(n)}function n(r,n){return r.replace(RegExp(n+"*[01]{8}"+n+"*","g"),function(r){return String.fromCharCode(parseInt(r,2))})}var t="www.WHAK.com",e="\n",o=r(t,e),a=n(o,e);alert(o+" = "+a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment