How to convert decimal to hexadecimal in JavaScript

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

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

27条回答
  •  醉酒成梦
    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.

提交回复
热议问题