-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
60 lines (58 loc) · 1.28 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @param {number} num
* @return {string}
*/
// var toHex = function (num) {
// const ans = Array.from({ length: 8 }, () => 0)
// let positive = true
// if (num < 0) {
// positive = false
// num = -num
// }
// let i = 0
// while (num > 0) {
// ans[i++] = num % 16
// num = Math.floor(num / 16)
// }
// if (!positive) {
// let rem = 0
// for (let i = 0; i < 8; i++) {
// let complement = 0 + rem
// ans[i] = complement - ans[i]
// if (ans[i] < 0) {
// rem = -1
// ans[i] += 16
// }
// }
// }
// if (ans.every(x => x === 0)) {
// return '0'
// }
// while (ans.length > 0 && ans[ans.length - 1] === 0) {
// ans.pop()
// }
// return ans.reverse().map(item => {
// if (item < 10) {
// return '' + item
// } else {
// return String.fromCharCode('a'.charCodeAt(0) + item - 10)
// }
// }).join('')
// }
var toHex = function (num) {
if (num === 0) return '0'
const ans = []
while (num !== 0) {
ans.push(num & 15)
num = num >>> 4
}
return ans.reverse().map((item) => {
if (item < 10) {
return '' + item
} else {
return String.fromCharCode('a'.charCodeAt(0) + item - 10)
}
}).join('')
}
console.log(toHex(-100))
console.log(toHex(100))