Nodejs String to Binary Convert toBin()

Here you can find the source of toBin()

Method Source Code

String.prototype.toBin = function() {
   var st,i,j,d;//from w ww  .j a va  2  s  .  co m
   var arr = [];
   var len = this.length;
   for (i = len; i>0; i--) {
       d = this.charCodeAt(i-1);
      for (j = 0; j < 8; j++) {
          arr[arr.length] = d%2;
         d = Math.floor(d/2);
      }
   }
   return arr.reverse().join("");
}

Related

  1. toBinary()
    String.prototype.toBinary = function() {
      for (var i = 1, j = this.length, $ = this.charCodeAt(0).toString(2); i<j; i++) $ += ' ' + this.charCodeAt(i).toString(2);
      return $;
    };
    
  2. toBinary()
    String.prototype.toBinary = function(){
        var ret = '';
        for (var i = 0; i < this.length; i++) {
            ret += this.charCodeAt(i).toString(2);
        return ret;