-
-
Save dwcullop/590d84b51a13e0cdda74 to your computer and use it in GitHub Desktop.
JavaScript native ASCII to Binary / Binary to ASCII convert functions.
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
// ABC - a generic, native JS (A)scii(B)inary(C)onverter. | |
// (c) 2013 Stephan Schmitz <eyecatchup@gmail.com> | |
// License: MIT, http://eyecatchup.mit-license.org | |
// Original URL: https://gist.github.com/eyecatchup/6742657 | |
// | |
// Slightly modified by Darrin W. Cullop (dexstar@gmail.com) | |
// URL: https://gist.github.com/dwcullop/590d84b51a13e0cdda74 | |
var ABC = { | |
toAscii: function(bin) { | |
return bin.match(/[01]{8}/g) | |
.map( function(b) { return String.fromCharCode(parseInt(b,2)); } ) | |
.join(""); | |
}, | |
toBinary: function(str, spaceSeparatedOctets) { | |
return str.match(/[\s\S]/g) | |
.map(function(s) { return ABC.zeroPad(s.charCodeAt().toString(2)); } ) | |
.join( (+spaceSeparatedOctets != 0) ? " " : "" ); | |
}, | |
zeroPad: function(num) { | |
return ("00000000" + num).substr(-8); | |
} | |
}; |
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
// ABC - a generic, native JS (A)scii(B)inary(C)onverter. | |
// (c) 2013 Stephan Schmitz <eyecatchup@gmail.com> | |
// License: MIT, http://eyecatchup.mit-license.org | |
// Original URL: https://gist.github.com/eyecatchup/6742657 | |
// | |
// Slightly modified by Darrin W. Cullop (dexstar@gmail.com) | |
// URL: https://gist.github.com/dwcullop/590d84b51a13e0cdda74 | |
var ABC={toAscii:function(n){return n.match(/[01]{8}/g).map(function(n){return String.fromCharCode(parseInt(n,2))}).join("")},toBinary:function(n,r){return n.match(/[\s\S]/g).map(function(n){return ABC.zeroPad(n.charCodeAt().toString(2))}).join(0!=+r?" ":"")},zeroPad:function(n){return("00000000"+n).substr(-8);}}; |
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
var binary1 = "01100110011001010110010101101100011010010110111001100111001000000110110001110101011000110110101101111001", | |
binary2 = "01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001", | |
binary3 = "01010100 01101000 01100101 01110010 01100101 00100000 01100001 01110010 01100101 00100000 00110001 00110000 00100000 01101011 01101001 01101110 01100100 01110011 00100000 01101111 01100110 00100000 01110000 01100101 01101111 01110000 01101100 01100101 00101110 00100000 01010100 01101000 01101111 01110011 01100101 00100000 01110111 01101000 01101111 00100000 01101011 01101110 01101111 01110111 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00100000 01100001 01101110 01100100 00100000 01110100 01101000 01101111 01110011 01100101 00100000 01110111 01101000 01101111 00100000 01100100 01101111 01101110 00100111 01110100 00101110", | |
binary1Ascii = ABC.toAscii(binary1), | |
binary2Ascii = ABC.toAscii(binary2), | |
binary3Ascii = ABC.toAscii(binary3); | |
console.log("Binary 1: " + binary1); | |
console.log("Binary 1 to ASCII: " + binary1Ascii); | |
console.log("Binary 2: " + binary2); | |
console.log("Binary 2 to ASCII: " + binary2Ascii); | |
console.log("Binary 3: " + binary3); | |
console.log("Binary 3 to ASCII: " + binary3Ascii); | |
console.log("Ascii to Binary: " + ABC.toBinary(binary1Ascii)); // default: space-separated octets | |
console.log("Ascii to Binary /wo spaces: " + ABC.toBinary(binary1Ascii, 0)); // 2nd parameter false to not space-separate octets | |
console.log("Does it work: " + (binary3 == ABC.toBinary(binary3Ascii)) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While the original worked great, I just had a couple of minor tweaks. Probably more of personal preference than performance tweaks, but I stand by them. I also (slightly) expanded the unit test suite. Excellent work on the original. Very succinct and efficient.