Created
March 26, 2011 16:17
-
-
Save fudini/888410 to your computer and use it in GitHub Desktop.
convert hex to decimal
This file contains hidden or 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
function hex2dec(hex) { | |
hex = hex.toLowerCase(hex).split(''); | |
var dec = 0; | |
while(h = hex.shift()) { | |
var code = h.charCodeAt(0); | |
dec = dec << 4; | |
dec += code < 58 ? code - 48 : code - 87; | |
} | |
return dec; | |
} | |
function hex2dec(hex) { | |
return parseInt(hex, 16); | |
} | |
function dec2hex(dec) { | |
if(dec == 0) return "0"; | |
var hex = ""; | |
while(dec) { | |
var b = dec & 0xf; | |
hex = (b < 10 ? b : String.fromCharCode(b + 87)) + hex; | |
dec = dec >> 4; | |
} | |
return hex; | |
} | |
function dec2hex(dec, padding) { | |
var hex = ""; | |
if(dec == 0) { | |
hex = "0"; | |
} else { | |
while(dec) { | |
var b = dec & 0xf; | |
hex = (b < 10 ? b : String.fromCharCode(b + 87)) + hex; | |
dec = dec >> 4; | |
} | |
} | |
var padder = "0000000000"; | |
return (padder + hex).substr(padder.length - padding + hex.length, padding); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment