Last active
August 29, 2015 14:20
-
-
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
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minified version: