-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add binary to BCD conversion algorithm #1509
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.