forked from ALLZ/hex-bin_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_to_bin.py
30 lines (26 loc) · 950 Bytes
/
hex_to_bin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sublime
import sublime_plugin
class HexToBinCommand(sublime_plugin.TextCommand):
MAX_STR_LEN = 20
def run(self, edit):
v = self.view
reglist = list(v.sel())
for j in range(0, len(reglist)):
hx = v.substr(v.sel()[j])
hx = hx.strip()
hexdig = '0123456789abcdefABCDEF'
l = True
if hx == '':
l = False
for i in hx:
if not(i in hexdig):
l = False
if l:
v.replace(edit, v.sel()[j], '{0:b}'.format(int(hx, 16)))
else:
if len(hx) > self.MAX_STR_LEN:
logMsg = hx[0:self.MAX_STR_LEN] + "..."
else:
logMsg = hx
sublime.status_message("\"%s\" isn't a hexadecimal number!" % logMsg)
sublime.error_message("\"%s\" isn't a hexadecimal number!" % logMsg)