File tree Expand file tree Collapse file tree 4 files changed +72
-7
lines changed
Expand file tree Collapse file tree 4 files changed +72
-7
lines changed Original file line number Diff line number Diff line change 1- export const binaryToDecimal = ( binaryString ) => {
1+ export default function binaryToDecimal ( binaryString ) {
22 let decimalNumber = 0
33 const binaryDigits = binaryString . split ( '' ) . reverse ( ) // Splits the binary number into reversed single digits
44 binaryDigits . forEach ( ( binaryDigit , index ) => {
55 decimalNumber += binaryDigit * ( Math . pow ( 2 , index ) ) // Summation of all the decimal converted digits
66 } )
77 return decimalNumber
88}
9-
10- // > binaryToDecimal('111001')
11- // 57
12-
13- // > binaryToDecimal('101')
14- // 5
Original file line number Diff line number Diff line change 1+ const binLookup = ( c ) => {
2+ switch ( c . toLowerCase ( ) ) {
3+ case '0' : return '0000'
4+ case '1' : return '0001'
5+ case '2' : return '0010'
6+ case '3' : return '0011'
7+ case '4' : return '0100'
8+ case '5' : return '0101'
9+ case '6' : return '0110'
10+ case '7' : return '0111'
11+ case '8' : return '1000'
12+ case '9' : return '1001'
13+ case 'a' : return '1010'
14+ case 'b' : return '1011'
15+ case 'c' : return '1100'
16+ case 'd' : return '1101'
17+ case 'e' : return '1110'
18+ case 'f' : return '1111'
19+ default : return ''
20+ }
21+ }
22+ const hexToBinary = ( hexString ) => {
23+ /*
24+ Function for convertung Hex to Binary
25+
26+ 1. We convert every hexadecimal bit to 4 binary bits
27+ 2. Conversion goes by searching in the lookup table
28+
29+ */
30+
31+ let result = ''
32+ hexString = hexString . split ( '' )
33+ hexString . forEach ( c => { result += binLookup ( c ) } )
34+ return result
35+ }
36+
37+ export default hexToBinary
Original file line number Diff line number Diff line change 1+ import binaryToDecimal from '../BinaryToDecimal'
2+
3+ describe ( 'BinaryToDecimal' , ( ) => {
4+ it ( 'expects to return correct decimal value' , ( ) => {
5+ expect ( binaryToDecimal ( '1000' ) ) . toBe ( 8 )
6+ } )
7+
8+ it ( 'expects to return correct hexadecimal value for more than one hex digit' , ( ) => {
9+ expect ( binaryToDecimal ( '01101000' ) ) . toBe ( 104 )
10+ } )
11+
12+ it ( 'expects to return correct hexadecimal value for padding-required binary' , ( ) => {
13+ expect ( binaryToDecimal ( '1000101' ) ) . toBe ( 69 )
14+ } )
15+ } )
Original file line number Diff line number Diff line change 1+ import hexToBinary from '../HexToBinary'
2+
3+ describe ( 'hexToBinary' , ( ) => {
4+ it ( 'expects to return correct hexadecimal value' , ( ) => {
5+ expect ( hexToBinary ( '8' ) ) . toBe ( '1000' )
6+ } )
7+
8+ it ( 'expects to return correct binary value for more than one hex digit' , ( ) => {
9+ expect ( hexToBinary ( 'EA' ) ) . toBe ( '11101010' )
10+ } )
11+
12+ it ( 'expects to test its robustness as it should be case-insensitive' , ( ) => {
13+ expect ( hexToBinary ( '4d' ) ) . toBe ( '01001101' )
14+ } )
15+
16+ it ( 'expects to return correct hexadecimal value, matching (num).toString(2)' , ( ) => {
17+ expect ( hexToBinary ( 'F' ) ) . toBe ( parseInt ( 'F' , 16 ) . toString ( 2 ) )
18+ } )
19+ } )
You can’t perform that action at this time.
0 commit comments