Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Forked from belohlavek/binaryUtil.js
Created January 10, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinrhode2/74b1a8afeadc17c7b2e65fb591133099 to your computer and use it in GitHub Desktop.
Save devinrhode2/74b1a8afeadc17c7b2e65fb591133099 to your computer and use it in GitHub Desktop.
ASCII to Binary and Binary to ASCII Utility functions in Javascript.
var Util = {
toBinary: function(input) {
var result = "";
for (var i = 0; i < input.length; i++) {
var bin = input[i].charCodeAt().toString(2);
result += Array(8 - bin.length + 1).join("0") + bin;
}
return result;
},
toAscii: function(input) {
var result = "";
var arr = input.match(/.{1,8}/g);
for (var i = 0; i < arr.length; i++) {
result += String.fromCharCode(parseInt(arr[i], 2).toString(10));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment