Nodejs String to Byte Convert getBytes()

Here you can find the source of getBytes()

Method Source Code

var crypto = require('crypto')

String.prototype.getBytes = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));//from   w w w . j a va  2 s  . com
  }
  return bytes;
};

module.exports = function convert(secretKey) {
  var message = 'SendRawEmail'
  var version = new Buffer([0x02])
  var signature = crypto.createHmac('sha256', new Buffer(secretKey.getBytes())).update(message).digest()
  var sigAndVer = Buffer.concat([version, signature])
  
  return sigAndVer.toString('base64')
}

Related

  1. getBytes()
    String.prototype.getBytes = function() {   
          var cArr = this.match(/[^\x00-\xff]/ig);   
          return this.length + (cArr == null ? 0 : cArr.length*2);   
    
  2. getBytes()
    String.prototype.getBytes = function () {
        var bytes = [];
        for (var i = 0; i < this.length; i++) {
            bytes.push(this.charCodeAt(i));
        return bytes;
    };
    
  3. getBytes()
    function addAll(arr1, arr2) {
      var i=0;
      for(i=0;i<arr2.length;i++) {
        arr1.push(arr2[i]);
    String.prototype.getBytes = function() {
        return encodeURIComponent(this).replace(/%../g, 'x').length;
    };
    ...
    
  4. toByteString()
    String.prototype.toByteString = function()
       var ar = this.split(' '),
         res = "";
       for(var i = 0; i < ar.length; ++i)
        res += String.fromCharCode( parseInt(ar[i], 16) );
       return res;
    ...
    
  5. toBytes()
    String.prototype.toBytes = function () {
      var bytes = [];
      for (var i = 0; i < this.length; ++i) {
          bytes.push(this.charCodeAt(i));
      return bytes;
    };
    
  6. toBytes()
    String.prototype.toBytes = function () {
      var i, ii, bytes = [];
      for (i = 0, ii = this.length; i < ii; i += 1) {
        bytes.push(this.charCodeAt(i));
      return bytes;
    };
    
  7. toBytes()
    String.prototype.toBytes = String.prototype.toBytes || function () {
        var BYTE = 8,
            BYTES = 2,
            MAX = Math.pow(2,BYTE)-1,
            c = '',
            buf,
            len = this.length,
            bytes = [];
        for (var i = 0; i < len; i++) {
    ...