hex8 = function(val) {
    var n = val & 0xFF,
        str = n.toString(16).toUpperCase()
        ;

    while(str.length < 2)
        str = "0" + str;

    return str;
}
Convert value as 8-bit unsigned integer to 2 digit hexadecimal number.