Nodejs Number to Hex Convert toHex(length)

Here you can find the source of toHex(length)

Method Source Code

/***********************************************
*   Number display methods//from  www  .ja  v a 2  s  .  c  om
************************************************/

Number.prototype.toHex = function(length) {
   var str = this.toString(16);
   while (str.length < length)
      str = '0' + str;
   str = str.toUpperCase();
   return str;
};

Number.prototype.toChar = function() {
   return String.fromCharCode(this);
};

/***********************************************
*   Number modification methods
************************************************/

Number.prototype.mod = function(num) {
   return ((this%num)+num)%num;
};
   
Number.prototype.byteWrap = function(bits) {
   return this.mod(1 << bits);
};

Number.prototype.bitSet = function(bit) {
   return this | (1<<bit);
};

Number.prototype.bitUnset = function(bit) {
   return this & ~(1<<bit);
};

Number.prototype.bitToggle = function(bit) {
   return this ^ (1<<bit);
};

Related

  1. toHex()
    Number.prototype.toHex = function() {
      var hex = this.toString(16);
      if (hex.length == 1) {
        hex = "0" + hex;
      return hex;
    
  2. toHex()
    Number.prototype.toHex = function () {
      var upper = (this.valueOf() >> 4) & 0x0F,
        lower = this.valueOf() & 0x0F;
      return upper.toString(16) + lower.toString(16);
    };
    
  3. toHex()
    Number.prototype.toHex = function() 
      try
        var val = this.valueOf();
        val = parseInt(val);
        val = Math.max(0,val);
        val = Math.min(val,255);
        val = Math.round(val);
    ...
    
  4. toHex()
    Number.prototype.toHex = function() {
      if (this < 16) {
        return '0' + this.toString(16); 
      return this.toString(16);
    
  5. toHex(len)
    "use strict";
    Number.prototype.toHex = function(len) {
        var str = this.toString(16);
        str = str.toUpperCase();
        str = str.substr(0, len);
        while(str.length < len) {
            str = "0" + str;
        return str;
    ...
    
  6. toHex(offset)
    Number.prototype.toHex = function (offset) {
      return this.toString(16).toUpperCase()
    };
    
  7. toHexByte()
    Number.prototype.toHexByte = function() {
        var s = this.toString(16).toUpperCase();
        if (s.length == 1) {
            s = "0" + s;
        return ">" + s;
    };
    
  8. toHexByteShort()
    Number.prototype.toHexByteShort = function() {
        var s = this.toString(16).toUpperCase();
        if (s.length == 1) {
            s = "0" + s;
        return s;
    };
    
  9. toHexWord()
    Number.prototype.toHexWord = function() {
        var s = this.toString(16).toUpperCase();
        while (s.length < 4) {
            s = "0" + s;
        return ">" + s;
    };