Javascript String toBytes()

Description

Javascript String toBytes()


String.prototype.toBytes = function() {
 var bytes = [];//ww  w  .  j  a va2s.  co  m
 for ( var i = 0; i < this.length; i += 1 ) {
  bytes.push( this.charCodeAt( i ) );
 }
 return bytes;
};

Javascript String toBytes()

String.prototype.toBytes = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
      bytes.push(this.charCodeAt(i));/*from w  w  w .  j a v  a  2s .co m*/
  }
  return bytes;
};

Javascript String toBytes()

//  `String` polyfills
// Convert string to charcode array
String.prototype.toBytes = function () {
 var i, ii, bytes = [];
 for (i = 0, ii = this.length; i < ii; i += 1) {
  bytes.push(this.charCodeAt(i));/*  ww w .ja  v a2  s .  com*/
 }
 return bytes;
};

// Convert charcode array to string
String.fromBytes = function (bytes) {
 var i, ii, result = '';
 for (i = 0, ii = bytes.length; i < ii; i += 1) {
  result += String.fromCharCode(bytes[i]);
 }
 return result;
};



PreviousNext

Related