Created
March 2, 2014 23:54
-
-
Save alexanderjeurissen/9315869 to your computer and use it in GitHub Desktop.
String prototype that converts the given message to a binary String.
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
//coffescript version | |
String::toBinary = -> | |
@split("").reduce (result, char) -> | |
bin = char.charCodeAt(0).toString(2) | |
result += "00000000".slice(0, 8 - bin.length) + bin | |
, "" |
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
// 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; | |
}; |
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
// 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