简体   繁体   中英

How to convert negative decimal to hexadecimal?

hex = Number(-59).toString(16)

hex is -3b

hex should be ffffffffffffffC5

Thanks for any help!

If the number is negative, the sign is preserved. Especially if the radix is 2, it's returning the binary (zeros and ones) of the number preceeded by a - sign but the two's complement.

This is how the toString() method of the Number type works, it doesn't output the two's complement.

In other words, the toString() method converts the number as if it was positive displaying its hexadecimal representation, if that number is negative it just puts a minus - before it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM