Created
November 30, 2012 10:49
-
-
Save CatTail/4175093 to your computer and use it in GitHub Desktop.
Javascript: convert decimal into hexadecimal
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
/** | |
* @param {number} dec Decimal number need to be converted | |
* @return {string} String representation of hexadecimal | |
*/ | |
var dec2hex = function(dec) { | |
var buf = [], | |
map = '0123456789ABCDEF'; | |
while (parseInt(dec / 16, 10) !== 0) { | |
buf.unshift(map[dec % 16]); | |
dec = parseInt(dec / 16, 10); | |
} | |
buf.unshift(map[dec % 16]); | |
return buf.join(''); | |
}; | |
var dec2hex2 = function(dec) { | |
var hex = [], | |
HEX = '0123456789ABCDEF'; | |
do { | |
hex.unshift(HEX[dec & 0xF]); | |
} while ( (dec = dec >> 4) !== 0 ); | |
return hex.join(''); | |
}; | |
var dec2hex3 = function(dec) { | |
return dec.toString(16); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment