How to convert decimal to hexadecimal in JavaScript

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

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

27条回答
  •  旧时难觅i
    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);
    }
    

提交回复
热议问题