Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ Highlight a number in the editor, then use the ket shortcut or choose from the
menu to toggle the conversion.

![screenshot](https://github.com/zaidt/hexbin/blob/master/resources/hexbin.png?raw=true)

For convert 8bit binary to hex write it in format:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"To" instead of "For"

```
0b00000001
```
21 changes: 20 additions & 1 deletion lib/hexbin-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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";
}
}
Expand All @@ -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;
Expand Down