Nodejs String to Binary Convert toBinary()

Here you can find the source of toBinary()

Method Source Code

// Method to encode a string as a series of bits
// 'Seth'.toBinary(); returns '1010011 1100101 1110100 1101000'
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 $;//from  ww  w  .ja  v a 2 s  .c o m
};

Related

  1. toBin()
    String.prototype.toBin = function() {
      var st,i,j,d;
      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);
    ...
    
  2. toBinary()
    String.prototype.toBinary = function(){
        var ret = '';
        for (var i = 0; i < this.length; i++) {
            ret += this.charCodeAt(i).toString(2);
        return ret;