// Input: // 26 // Output: // "1a" // Input: // -1 // Output: // "ffffffff" //使用js自带方法 //return (num>>>0).toString(16); //不使用 var toHex = function(num) { var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; var s = (num >>> 0).toString(2), ss = ''; for (var i = s.length; i > 0; i -= 4) { ss = arr[parseInt(s.substring(i - 4, i), 2)] + ss; //console.log(ss) } return ss; }; console.log(toHex(26)); console.log(toHex(-26)); console.log(toHex(-1));