antares/src/common/libs/hexToBinary.ts

35 lines
548 B
TypeScript
Raw Normal View History

2020-06-18 19:01:09 +02:00
const lookup = {
0: '0000',
1: '0001',
2: '0010',
3: '0011',
4: '0100',
5: '0101',
6: '0110',
7: '0111',
8: '1000',
9: '1001',
a: '1010',
b: '1011',
c: '1100',
d: '1101',
e: '1110',
f: '1111',
A: '1010',
B: '1011',
C: '1100',
D: '1101',
E: '1110',
F: '1111'
2022-05-10 12:57:25 +02:00
} as const;
2022-05-10 13:22:26 +02:00
export type HexChar = keyof typeof lookup
2020-06-18 19:01:09 +02:00
2022-05-10 12:57:25 +02:00
export default function hexToBinary (hex: HexChar[]) {
2020-06-18 19:01:09 +02:00
let binary = '';
for (let i = 0; i < hex.length; i++)
2020-06-18 19:01:09 +02:00
binary += lookup[hex[i]];
return binary;
}