-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added Hex to Binary conversion #805
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de3e2f1
Added Hex to Binary conversion
ddhira123 1432037
Update Conversions/HexToBinary.js
ddhira123 6efa3db
Update Conversions/HexToBinary.js
ddhira123 3123322
Update Conversions/HexToBinary.js
ddhira123 b70af8d
Update Conversions/HexToBinary.js
ddhira123 b4814b1
Merge branch 'master' into master
ddhira123 45d14b7
Fix errors
ddhira123 5dbc20f
fix: typo
raklaptudirm 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,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 = '' | ||
ddhira123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hexString = hexString.split('') | ||
ddhira123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hexString.forEach(c => { result += binLookup(c) }) | ||
ddhira123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return result | ||
ddhira123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export default hexToBinary |
19 changes: 19 additions & 0 deletions
Conversions/test/HexToBinary.test.js
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,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)) | ||
}) | ||
}) |
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.