Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Created March 2, 2014 23:54
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 alexanderjeurissen/9315869 to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/9315869 to your computer and use it in GitHub Desktop.
String prototype that converts the given message to a binary String.
//coffescript version
String::toBinary = ->
@split("").reduce (result, char) ->
bin = char.charCodeAt(0).toString(2)
result += "00000000".slice(0, 8 - bin.length) + bin
, ""
// IE8 compatible version
String.prototype.toBinary = function(){
var result;
for(var i = 0, l = this.length; i<l; i++){
var bin = this.charCodeAt(i).toString(2);
result += '00000000'.slice(0,8-bin.length)+bin;
}
return result;
};
// ie9 and higher version
String.prototype.toBinary = function() {
return this.split('').reduce(function(result,char){
var bin = char.charCodeAt(0).toString(2);
return (result += '00000000'.slice(0,8-bin.length)+bin);
},'');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment