How to convert decimal to hexadecimal in JavaScript

后端 未结 27 2634
臣服心动
臣服心动 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);
    }
    

提交回复
热议问题