diff --git a/Conversions/BinaryToDecimal.js b/Conversions/BinaryToDecimal.js index 744bfc2bc..f5a908658 100644 --- a/Conversions/BinaryToDecimal.js +++ b/Conversions/BinaryToDecimal.js @@ -1,4 +1,4 @@ -export const binaryToDecimal = (binaryString) => { +export default function binaryToDecimal (binaryString) { let decimalNumber = 0 const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits binaryDigits.forEach((binaryDigit, index) => { @@ -6,9 +6,3 @@ export const binaryToDecimal = (binaryString) => { }) return decimalNumber } - -// > binaryToDecimal('111001') -// 57 - -// > binaryToDecimal('101') -// 5 diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js new file mode 100644 index 000000000..b67b00c82 --- /dev/null +++ b/Conversions/HexToBinary.js @@ -0,0 +1,37 @@ +const binLookup = (c) => { + switch (c.toLowerCase()) { + case '0': return '0000' + case '1': return '0001' + case '2': return '0010' + case '3': return '0011' + case '4': return '0100' + case '5': return '0101' + case '6': return '0110' + case '7': return '0111' + case '8': return '1000' + case '9': return '1001' + case 'a': return '1010' + case 'b': return '1011' + case 'c': return '1100' + case 'd': return '1101' + case 'e': return '1110' + case 'f': return '1111' + default: return '' + } +} +const hexToBinary = (hexString) => { + /* + Function for convertung Hex to Binary + + 1. We convert every hexadecimal bit to 4 binary bits + 2. Conversion goes by searching in the lookup table + + */ + + let result = '' + hexString = hexString.split('') + hexString.forEach(c => { result += binLookup(c) }) + return result +} + +export default hexToBinary diff --git a/Conversions/test/BinaryToDecimal.test.js b/Conversions/test/BinaryToDecimal.test.js new file mode 100644 index 000000000..97f23aeb4 --- /dev/null +++ b/Conversions/test/BinaryToDecimal.test.js @@ -0,0 +1,15 @@ +import binaryToDecimal from '../BinaryToDecimal' + +describe('BinaryToDecimal', () => { + it('expects to return correct decimal value', () => { + expect(binaryToDecimal('1000')).toBe(8) + }) + + it('expects to return correct hexadecimal value for more than one hex digit', () => { + expect(binaryToDecimal('01101000')).toBe(104) + }) + + it('expects to return correct hexadecimal value for padding-required binary', () => { + expect(binaryToDecimal('1000101')).toBe(69) + }) +}) diff --git a/Conversions/test/HexToBinary.test.js b/Conversions/test/HexToBinary.test.js new file mode 100644 index 000000000..40148d1ce --- /dev/null +++ b/Conversions/test/HexToBinary.test.js @@ -0,0 +1,19 @@ +import hexToBinary from '../HexToBinary' + +describe('hexToBinary', () => { + it('expects to return correct hexadecimal value', () => { + expect(hexToBinary('8')).toBe('1000') + }) + + it('expects to return correct binary value for more than one hex digit', () => { + expect(hexToBinary('EA')).toBe('11101010') + }) + + it('expects to test its robustness as it should be case-insensitive', () => { + expect(hexToBinary('4d')).toBe('01001101') + }) + + it('expects to return correct hexadecimal value, matching (num).toString(2)', () => { + expect(hexToBinary('F')).toBe(parseInt('F', 16).toString(2)) + }) +})