-
Notifications
You must be signed in to change notification settings - Fork 1
Add convert from 8bit binary to hex #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,12 @@ export default class HexbinView { | |
| hex = '0x' + this.dec2hex(dec); | ||
| } | ||
| this.element.textContent = "Hex:" + hex + " Bin:" + this.formatBinary(bin) + " Dec:" + dec; | ||
| } else { | ||
| } else if (selection.length == 10 && selection.toUpperCase().substr(-10, 2) == '0B') { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please make the length variable instead of 10? i.e. a values "0b11011" should be valid. btw, line 28 "dec = parseInt(selection);" validates if the string is a number, if it fails the "if" is not entered, so you just need to check if te first 2 chars are "0b" |
||
| bin = selection.substr(2, 10); | ||
| hex = this.bin2hex(bin); | ||
| dec = this.bin2dec(bin); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a specific reason why the dec needs to be re-set? the decimal value is already set at line 28 "dec = parseInt(selection);" |
||
| this.element.textContent = "Hex:" + hex + " Bin:" + this.formatBinary(bin) + " Dec:" + dec; | ||
| } else { | ||
| this.element.textContent = "Invalid"; | ||
| } | ||
| } | ||
|
|
@@ -49,6 +54,20 @@ export default class HexbinView { | |
| return (+dec).toString(16); | ||
| } | ||
|
|
||
| bin2dec(bin){ | ||
| return parseInt(bin, 2).toString(10); | ||
| } | ||
|
|
||
| bin2hex(bin) { | ||
| hex = parseInt(bin, 2).toString(16); | ||
|
|
||
| if(hex.toString().length > 1) { | ||
| return "0x" + hex.toString().toUpperCase(); | ||
| } | ||
|
|
||
| return "0x0" + hex.toString().toUpperCase(); | ||
| } | ||
|
|
||
| formatBinary(bin) { | ||
| str = ""; | ||
| remainder = bin.length % 8; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"To" instead of "For"