How to convert decimal to hexadecimal in JavaScript

后端 未结 27 2586
臣服心动
臣服心动 2020-11-21 23:05

How do you convert decimal values to their hexadecimal equivalent in JavaScript?

相关标签:
27条回答
  • 2020-11-21 23:28
    function dec2hex(i)
    {
      var result = "0000";
      if      (i >= 0    && i <= 15)    { result = "000" + i.toString(16); }
      else if (i >= 16   && i <= 255)   { result = "00"  + i.toString(16); }
      else if (i >= 256  && i <= 4095)  { result = "0"   + i.toString(16); }
      else if (i >= 4096 && i <= 65535) { result =         i.toString(16); }
      return result
    }
    
    0 讨论(0)
  • 2020-11-21 23:31

    Convert a number to a hexadecimal string with:

    hexString = yourNumber.toString(16);
    

    And reverse the process with:

    yourNumber = parseInt(hexString, 16);
    
    0 讨论(0)
  • 2020-11-21 23:31

    If you need to handle things like bit fields or 32-bit colors, then you need to deal with signed numbers. The JavaScript function toString(16) will return a negative hexadecimal number which is usually not what you want. This function does some crazy addition to make it a positive number.

    function decimalToHexString(number)
    {
      if (number < 0)
      {
        number = 0xFFFFFFFF + number + 1;
      }
    
      return number.toString(16).toUpperCase();
    }
    
    console.log(decimalToHexString(27));
    console.log(decimalToHexString(48.6));

    0 讨论(0)
  • 2020-11-21 23:33

    The code below will convert the decimal value d to hexadecimal. It also allows you to add padding to the hexadecimal result. So 0 will become 00 by default.

    function decimalToHex(d, padding) {
        var hex = Number(d).toString(16);
        padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
    
        while (hex.length < padding) {
            hex = "0" + hex;
        }
    
        return hex;
    }
    
    0 讨论(0)
  • 2020-11-21 23:33

    For completeness, if you want the two's-complement hexadecimal representation of a negative number, you can use the zero-fill-right shift >>> operator. For instance:

    > (-1).toString(16)
    "-1"
    
    > ((-2)>>>0).toString(16)
    "fffffffe"
    

    There is however one limitation: JavaScript bitwise operators treat their operands as a sequence of 32 bits, that is, you get the 32-bits two's complement.

    0 讨论(0)
  • 2020-11-21 23:35

    As the accepted answer states, the easiest way to convert from decimal to hexadecimal is var hex = dec.toString(16). However, you may prefer to add a string conversion, as it ensures that string representations like "12".toString(16) work correctly.

    // Avoids a hard-to-track-down bug by returning `c` instead of `12`
    (+"12").toString(16);
    

    To reverse the process you may also use the solution below, as it is even shorter.

    var dec = +("0x" + hex);
    

    It seems to be slower in Google Chrome and Firefox, but is significantly faster in Opera. See http://jsperf.com/hex-to-dec.

    0 讨论(0)
提交回复
热议问题