Nodejs Number to Hex Convert hex(len)

Here you can find the source of hex(len)

Method Source Code

Number.prototype.hex = function(len) {
   var str = this.toString(16).toUpperCase();
   /* ww  w .j a v  a2s  .co  m*/
   if(len != null)
      str = (Array(len).join('0') + str).slice(-len);
   
   return '0x' + str;
}

var utils = {
   toHex: function(str) {
      return '0x' + Number(str.charCodeAt(0)).toString(16).toUpperCase();
   }
}

Related

  1. toHexByte()
    Number.prototype.toHexByte = function() {
        var s = this.toString(16).toUpperCase();
        if (s.length == 1) {
            s = "0" + s;
        return ">" + s;
    };
    
  2. toHexByteShort()
    Number.prototype.toHexByteShort = function() {
        var s = this.toString(16).toUpperCase();
        if (s.length == 1) {
            s = "0" + s;
        return s;
    };
    
  3. toHexWord()
    Number.prototype.toHexWord = function() {
        var s = this.toString(16).toUpperCase();
        while (s.length < 4) {
            s = "0" + s;
        return ">" + s;
    };
    
  4. to_hex()
    Number.prototype.to_hex = function() {
      if (this === 0) {
        return "00";
      var chars = "0123456789ABCDEF";
      var n = Math.max(0, this);
      n = Math.min(n, 255);
      n = Math.round(n);
      return chars.charAt((n - n % 16)/16) + chars.charAt(n % 16);
    ...
    
  5. hex()
    Number.prototype.hex = function () {
      return "0x" + this.toString(16);
    };
    
  6. printHex(bits)
    Number.prototype.printHex = function(bits)
      var zeros = '';
      var len = bits/4;
      for(var i=0;i<len;i++)
        zeros+='0';  
      return (""+zeros + this.toString(16)).substr(-1*len).toUpperCase();