Skip to content

Instantly share code, notes, and snippets.

@jmsdnns
Created August 24, 2017 17:35
Show Gist options
  • Save jmsdnns/e4ed0f5ea774b90eeb55a20ca456aa2d to your computer and use it in GitHub Desktop.
Save jmsdnns/e4ed0f5ea774b90eeb55a20ca456aa2d to your computer and use it in GitHub Desktop.
decimal-to-hex and hex-to-decimal conversion in javascript
(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