Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add binary to BCD conversion algorithm #1509

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Conversions/BinaryToBcd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Generates a Binary Coded Decimal (BCD 4-bit representation) equivalent of given binary number.
* @param {string} binaryInput - Binary number in string format to be converted to BCD.
* @returns {string} - A string equivalent of binary coded decimal (4-bit representation).
*
* @see
* For more info: https://en.wikipedia.org/wiki/Binary-coded_decimal
*
* @example
* const binaryInput = '1100010010'
* const bcdNum = binaryToBcd(decimal)
* // bcdNum will be '11110000110'
*/

const bcdEquivalent = (decimalCharacter) => {
switch (decimalCharacter) {
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'
}
}

const isValidBinary = (binaryInput) => {
for (let i = 0; i < binaryInput.length; i++) {
if (binaryInput[i] !== '0' && binaryInput[i] !== '1') {
return false
}
}
return true
}

const binaryToBcd = (binaryInput) => {
if (!isValidBinary(binaryInput)) {
throw new Error('Input is not a valid binary number.')
}

let decimalInput = parseInt(binaryInput, 2)
let decimalString = decimalInput.toString()
let bcdNum = ''
for (let i = 0; i < decimalString.length; i++) {
bcdNum += bcdEquivalent(decimalString[i])
}

// Remove unnecessary trailing zeros
let tempDecimal = parseInt(bcdNum, 2)
bcdNum = tempDecimal.toString(2)

return bcdNum
}

export { binaryToBcd }
25 changes: 25 additions & 0 deletions Conversions/test/BinaryToBcd.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { binaryToBcd } from '../BinaryToBcd'
describe('Binary To BCD', () => {
test.each([
['1101101001', '100001110011'],
['11101111', '1000111001'],
['1010100010110', '101001110011000'],
['1111101000', '1000000000000'],
['111111111111111', '110010011101100111']
])(
'Should return the equivalent BCD of the binary number.',
(binaryNum, expected) => {
expect(binaryToBcd(binaryNum)).toEqual(expected)
}
)

test.each([
['111100001230', 'Input is not a valid binary number.'],
['781df', 'Input is not a valid binary number.'],
['0897111', 'Input is not a valid binary number.'],
['Dsd1022', 'Input is not a valid binary number.'],
['00000000000@#$%', 'Input is not a valid binary number.']
])('Should return the error message.', (binaryNum, expected) => {
expect(() => binaryToBcd(binaryNum)).toThrowError(expected)
})
})