Javascript Number Integer Convert to Hex

Description

Javascript Number Integer Convert to Hex


function dec2Hex(dec) {
    dec = parseInt(dec, 10);/*  ww  w . ja  v a  2  s. c om*/
    if (!isNaN(dec)) {
        hexChars = "0123456789ABCDEF";
        if (dec > 255) {
            return "Out of Range";
        }
        var i = dec % 16;
        var j = (dec - i) / 16;
        result = "0x";
        result += hexChars.charAt(j) + hexChars.charAt(i);
        return result;
    } else {
        return NaN;
    }
}
console.log(dec2Hex(123) );
console.log(dec2Hex(12) );



PreviousNext

Related