JSFiddle - React, Tailwind, and code Playground
HTML
<p>Comparisons of the various answers to <a target="_blank" href="http://stackoverflow.com/q/57803">this Stack Overflow question</a>
</p>
<table>
<tbody id="comparison-table">
<tr id="headings">
<th>input</th>
<th>.toString(16)</th>
</tr>
</tbody>
</table>
JavaScript
var tests = [-1, 0, 2, 200, 201, 145000, 90000000];
var answers = {
"Tod": {
id: 697841,
func: function (i) {
return (i < 0 ? 0xFFFFFFFF + i + 1 : i).toString(16).toUpperCase();
}
},
"Luke Smith": {
id: 57807,
func: function (d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}
},
"quartzo": {
id: 6680530,
func: function (i) {
return ("0" + (Number(i).toString(16))).slice(-2).toUpperCase();
}
},
"Baznr": {
id: 13240395,
func: function (i) {
return (i + 0x10000).toString(16).substr(-4).toUpperCase();
}
},
"Alberto": {
id: 17106974,
func: function (i) {
return (i >>> 0).toString(16);
}
},
"mystifeid": {
id: 3689638,
func: function (i) {
var hex = Number(i).toString(16);
return "000000".substr(0, 6 - hex.length) + hex;
}
},
"korana": {
id: 11012314,
func: function (n) {
if (n < 0) {
n = 0xFFFFFFFF + n + 1;
}
return "0x" + ("00000000" + n.toString(16).toUpperCase()).substr(-8);
}
},
"Adamarla": {
id: 9034019,
func: function (decimal, chars) {
return (decimal + Math.pow(16, chars)).toString(16).slice(-chars).toUpperCase();
}
}
};
var headings = document.getElementById("headings");
for (var answer in answers) {
var id = answers[answer].id;
headings.innerHTML += "<th><a target='_blank' href='http://stackoverflow.com/a/" + id + "/2203482'>" + answer + "</th>";
}
var table = document.getElementById("comparison-table");
for (var i in tests) {
var dec = tests[i];
...