Created
August 22, 2012 23:04
-
-
Save amorri40/3430429 to your computer and use it in GitHub Desktop.
Javascript Byte to hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "A", "B", "C", "D", "E", "F"]; | |
function byteToHex(b) { | |
return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f]; | |
} |
Wonderful! This saved me a lot of work and headache.
It looks like ES6 has a handy way of achieving the same thing:
(yournumber).toString(16)
example:
(255).toString(16); "ff"
https://2ality.com/2015/04/numbers-math-es6.html#new-integer-literals
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant. This is just what I needed to solve a huge problem I was having! http://stackoverflow.com/questions/28706485/javascript-how-to-generate-rfc2898derivebytes-like-c