How to convert decimal to hexadecimal in JavaScript

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

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

27条回答
  •  遇见更好的自我
    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);
    }
    

提交回复
热议问题