Created
August 24, 2017 17:35
-
-
Save jmsdnns/e4ed0f5ea774b90eeb55a20ca456aa2d to your computer and use it in GitHub Desktop.
decimal-to-hex and hex-to-decimal conversion in javascript
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($) { | |
$.hexify = { | |
decimalToHex: function(decimal) { | |
var hex = decimal.toString(16); | |
hex = $.hexify.padPrefix(hex, 2, "0"); | |
return hex; | |
}, | |
hexToDecimal: function(hex) { | |
var decimal = parseInt(hex, 16); | |
return decimal; | |
}, | |
padPrefix: function(s, width, padChar) { | |
var paddedS = s; | |
if(s.length < width) { | |
var padding = new Array(width - paddedS.length + 1).join(padChar); | |
var paddedS = padding + s; | |
} | |
return paddedS; | |
}, | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment