Fork me on GitHub

After code
  • Geeker
  • Gamer
  • JS
  • C
  • Node
  • React
  • Hippop
  • TDD
That Is An Byte of Me

Number to Hex in JavaScript

12 Oct 2014

Hex String

Sometimes, Hex is usefull when debuging some value. Usually, we use the toString method of Number.

(255).toString(16) // 'ff'
(55).toString(16)  // '37'

But when it’s negative value, it failed.

(-1).toString(16) // '-1'

Bitwise Operation >>>

zero-fill right shift (>>>)is a nice solution to get the hex expresstion of negative value.

zero-fill right shift

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative.

As it’s description, if we do number>>>0, nothing will change to the value of the number; but if it’s a negative number, it will add new sign bit to it, so we can get the complement of the negative value, now we can use the toString(radix) method.

function toHex(num){
  return (num>>>0).toString(16);
}

toHex(255) // 'ff'
toHex(55)  // '37'
toHex(-1)  // 'ffffffff'

Ref

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#%3E%3E%3E_%28Zero-fill_right_shift%29


End

分享到: QQ空间 新浪微博 腾讯微博 微信 更多
comments powered by Disqus