Skip to content

Instantly share code, notes, and snippets.

@eyecatchup
Last active November 6, 2023 20:03
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save eyecatchup/6742657 to your computer and use it in GitHub Desktop.
Save eyecatchup/6742657 to your computer and use it in GitHub Desktop.
JavaScript native ASCII to Binary / Binary to ASCII convert functions.
// 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
// URL: https://gist.github.com/eyecatchup/6742657
var ABC = {
toAscii: function(bin) {
return bin.replace(/\s*[01]{8}\s*/g, function(bin) {
return String.fromCharCode(parseInt(bin, 2))
})
},
toBinary: function(str, spaceSeparatedOctets) {
return str.replace(/[\s\S]/g, function(str) {
str = ABC.zeroPad(str.charCodeAt().toString(2));
return !1 == spaceSeparatedOctets ? str : str + " "
})
},
zeroPad: function(num) {
return "00000000".slice(String(num).length) + num
}
};
// 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
// URL: https://gist.github.com/eyecatchup/6742657
var ABC={toAscii:function(a){return a.replace(/\s*[01]{8}\s*/g,function(a){return String.fromCharCode(parseInt(a,2))})},toBinary:function(a,b){return a.replace(/[\s\S]/g,function(a){a=ABC.zeroPad(a.charCodeAt().toString(2));return!1==b?a:a+" "})},zeroPad:function(a){return"00000000".slice(String(a).length)+a}};
var binary1 = "01100110011001010110010101101100011010010110111001100111001000000110110001110101011000110110101101111001",
binary2 = "01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001",
binary1Ascii = ABC.toAscii(binary1),
binary2Ascii = ABC.toAscii(binary2);
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("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
@albell
Copy link

albell commented Jul 18, 2015

Thanks, but this doesn't work. Try:

ABC.toAscii("11110101")

and you will get õ, which is not an ASCII character. ASCII encodes 128 specified characters into 7-bit binary integers. I mention because this is the top-ranked google result for this topic.

@PaulStaden
Copy link

http://www.theasciicode.com.ar/extended-ascii-code/lowercase-letter-o-tilde-o-tilde-ascii-code-228.html

Ascii code 228 which you will get with Alt + 'o'
And is part of the 'extended Ascii codes' running up to 255 starting with 0 so 8-bit totaling 256 characters the extended Ascii codes were added in 1981.

@hcortezr
Copy link

Nice! I Love This!

@CodeMaxx
Copy link

This is awesome! Thanks! 😄

@devinrhode2
Copy link

es6-ified:

          let ABC = {
            toAscii(bin) {
              return bin.replace(/\s*[01]{8}\s*/g, function(bin) {
                return String.fromCharCode(parseInt(bin, 2));
              });
            },
            toBinary(str, spaceSeparatedOctets) {
              return str.replace(/[\s\S]/g, function(str) {
                str = ABC.zeroPad(str.charCodeAt().toString(2));
                return !1 == spaceSeparatedOctets ? str : str + ' '
              });
            },
            zeroPad(num) {
              return '00000000'.slice(String(num).length) + num;
            }
          };

@iHype
Copy link

iHype commented Aug 21, 2018

i get error: UnhandledPromiseRejectionWarning: TypeError: str.replace is not a function

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