mirror of
https://github.com/javl/image2cpp.git
synced 2026-09-11 09:03:04 +00:00
Clean up and simplify our hexToBinary method
This commit is contained in:
+4
-31
@@ -467,40 +467,13 @@ function updateString(fieldName) {
|
||||
|
||||
// Convert hex to binary
|
||||
function hexToBinary(s) {
|
||||
let i;
|
||||
let ret = '';
|
||||
// lookup table for easier conversion. "0" characters are padded for "1" to "7"
|
||||
const lookupTable = {
|
||||
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',
|
||||
};
|
||||
for (i = 0; i < s.length; i += 1) {
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
if (lookupTable.hasOwnProperty(s[i])) {
|
||||
ret += lookupTable[s[i]];
|
||||
} else {
|
||||
for (let i = 0; i < s.length; i += 1) {
|
||||
const nibble = parseInt(s[i], 16);
|
||||
if (Number.isNaN(nibble)) {
|
||||
return { valid: false, s };
|
||||
}
|
||||
ret += nibble.toString(2).padStart(4, '0');
|
||||
}
|
||||
return { valid: true, result: ret };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user