How to convert decimal to hexadecimal in JavaScript

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

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

相关标签:
27条回答
  • 2020-11-21 23:45

    If you want to convert a number to a hexadecimal representation of an RGBA color value, I've found this to be the most useful combination of several tips from here:

    function toHexString(n) {
        if(n < 0) {
            n = 0xFFFFFFFF + n + 1;
        }
        return "0x" + ("00000000" + n.toString(16).toUpperCase()).substr(-8);
    }
    
    0 讨论(0)
  • 2020-11-21 23:47

    And if the number is negative?

    Here is my version.

    function hexdec (hex_string) {
        hex_string=((hex_string.charAt(1)!='X' && hex_string.charAt(1)!='x')?hex_string='0X'+hex_string : hex_string);
        hex_string=(hex_string.charAt(2)<8 ? hex_string =hex_string-0x00000000 : hex_string=hex_string-0xFFFFFFFF-1);
        return parseInt(hex_string, 10);
    }
    
    0 讨论(0)
  • 2020-11-21 23:47

    I haven't found a clear answer, without checks if it is negative or positive, that uses two's complement (negative numbers included). For that, I show my solution to one byte:

    ((0xFF + number +1) & 0x0FF).toString(16);
    

    You can use this instruction to any number bytes, only you add FF in respective places. For example, to two bytes:

    ((0xFFFF + number +1) & 0x0FFFF).toString(16);
    

    If you want cast an array integer to string hexadecimal:

    s = "";
    for(var i = 0; i < arrayNumber.length; ++i) {
        s += ((0xFF + arrayNumber[i] +1) & 0x0FF).toString(16);
    }
    
    0 讨论(0)
  • 2020-11-21 23:48

    In case you're looking to convert to a 'full' JavaScript or CSS representation, you can use something like:

      numToHex = function(num) {
        var r=((0xff0000&num)>>16).toString(16),
            g=((0x00ff00&num)>>8).toString(16),
            b=(0x0000ff&num).toString(16);
        if (r.length==1) { r = '0'+r; }
        if (g.length==1) { g = '0'+g; }
        if (b.length==1) { b = '0'+b; }
        return '0x'+r+g+b;                 // ('#' instead of'0x' for CSS)
      };
    
      var dec = 5974678;
      console.log( numToHex(dec) );        // 0x5b2a96
    
    0 讨论(0)
  • 2020-11-21 23:49

    I'm doing conversion to hex string in a pretty large loop, so I tried several techniques in order to find the fastest one. My requirements were to have a fixed-length string as a result, and encode negative values properly (-1 => ff..f).

    Simple .toString(16) didn't work for me since I needed negative values to be properly encoded. The following code is the quickest I've tested so far on 1-2 byte values (note that symbols defines the number of output symbols you want to get, that is for 4-byte integer it should be equal to 8):

    var hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
    function getHexRepresentation(num, symbols) {
        var result = '';
        while (symbols--) {
            result = hex[num & 0xF] + result;
            num >>= 4;
        }
        return result;
    }
    

    It performs faster than .toString(16) on 1-2 byte numbers and slower on larger numbers (when symbols >= 6), but still should outperform methods that encode negative values properly.

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

    Combining some of these good ideas for an RGB-value-to-hexadecimal function (add the # elsewhere for HTML/CSS):

    function rgb2hex(r,g,b) {
        if (g !== undefined)
            return Number(0x1000000 + r*0x10000 + g*0x100 + b).toString(16).substring(1);
        else
            return Number(0x1000000 + r[0]*0x10000 + r[1]*0x100 + r[2]).toString(16).substring(1);
    }
    
    0 讨论(0)
提交回复
热议问题