-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecToHex.ts
51 lines (47 loc) · 935 Bytes
/
decToHex.ts
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
import {
DivMod,
DivModResult
} from './calc/division'
import {
Base10,
Digits,
FromBase10,
ToBase10,
ToString
} from './calc/common';
export type DecToHex<T extends number> =
number extends T
? string
: T extends never
? never
: `${T}` extends `-${infer S}`
? `-${_DecToHex<ToBase10<S>>}`
: _DecToHex<ToBase10<ToString<T>>>;
type _10 = ToBase10<"16">;
type HexMap = [
"0", "1", "2", "3",
"4", "5", "6", "7",
"8", "9", "a", "b",
"c", "d", "e", "f"
];
type _DecToHex<
T extends Base10,
Acc extends string = ""
> =
DivMod<T, _10> extends
DivModResult<
infer Quotient,
infer Remainder
>
? Digits<Quotient> extends ["0"]
? `${ToIndex<Remainder>}${Acc}`
: _DecToHex<
Quotient,
`${ToIndex<Remainder>}${Acc}`
>
: never;
type ToIndex<T extends Base10> =
FromBase10<T> extends
`${infer K extends number}`
? HexMap[K]
: "";