(binaryString)
| 44 | } |
| 45 | } |
| 46 | const binaryToHex = (binaryString) => { |
| 47 | /* |
| 48 | Function for converting Binary to Hex |
| 49 | |
| 50 | 1. The conversion will start from Least Significant Digit (LSB) to the Most Significant Bit (MSB). |
| 51 | 2. We divide the bits into sections of 4-bits starting from LSB to MSB. |
| 52 | 3. If the MSB get less than 4 bits, then we pad 0s to the front of it. |
| 53 | |
| 54 | For Example: |
| 55 | Binary String = '1001101' |
| 56 | |
| 57 | 1. Divide it to 2 parts => ['100', '1101'] |
| 58 | 2. Pad 0s the MSB so it'll be => ['0100', '1101'] |
| 59 | 3. Use the lookup table and merge them, therefore the result is 4D. |
| 60 | |
| 61 | */ |
| 62 | |
| 63 | let result = '' |
| 64 | binaryString = binaryString.split('') |
| 65 | for (let i = binaryString.length - 1; i >= 0; i = i - 4) { |
| 66 | if (i >= 3) { |
| 67 | result += hexLookup(binaryString.slice(i - 3, i + 1).join('')) |
| 68 | } else { |
| 69 | result += hexLookup(binaryString.slice(0, i + 1).join('')) |
| 70 | } |
| 71 | } |
| 72 | return result.split('').reverse().join('') |
| 73 | } |
| 74 | |
| 75 | export default binaryToHex |
no test coverage detected