-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexBinDecConverter.py
94 lines (77 loc) · 3.67 KB
/
HexBinDecConverter.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
#
# HexBinDecConverter is a number converter for Sublime Text 3
#
# https://github.com/rohanrhu/HexBinDecConverter
#
# Licensed under MIT
# Copyright (C) 2018, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <rohanrhu2@gmail.com>
import sublime
import sublime_plugin
numbers = [str(i) for i in range(10)]
class HexBinDecConverterCommand(sublime_plugin.TextCommand):
def run(self, edit, from_event=False, show_all=False, point=False):
view = self.view
for region in view.sel():
if point:
wordloc = view.word(point.conjugate())
word = view.substr(wordloc)
else:
word = view.substr(view.word(region))
if word.__len__() > 0:
is_dec = False
is_hex = False
is_bin = False
if word.__len__() > 2 and word[:2] == '0x':
is_hex = True
decval = int(word[2:], 16)
elif word[0] in numbers:
if word[-1] == 'h' or word[-1] == 'H':
is_hex = True
decval = int(word[:-1], 16)
elif word[-1] == 'b' or word[-1] == 'B':
binary = word[:-1]
is_bin = True
for i in binary:
if i != '0' and i != '1':
is_bin = False
break
if is_bin:
decval = int(binary, 2)
if show_all or not from_event:
is_dec = True
for i in word:
if i not in numbers:
is_dec = False
break
if is_dec:
decval = int(word)
hexval = hex(decval)
elif is_bin:
hexval = hex(decval)
if is_dec or is_hex or is_bin:
binval = '{0:b}'.format(decval).zfill(64)
binval = binval[:8] + ' ' + binval[8:]
binval = binval[:17] + ' ' + binval[17:]
binval = binval[:26] + ' ' + binval[26:]
binval = binval[:35] + ' ' + binval[35:]
binval = binval[:44] + ' ' + binval[44:]
binval = binval[:53] + ' ' + binval[53:]
binval = binval[:62] + ' ' + binval[62:]
binval = binval[:36] + '<br> ' + binval[36:]
decval = str(decval)
loc = -1
if point:
loc = wordloc.end()
if is_hex:
view.show_popup('<b style="color: red;">Dec:</b> '+decval+'<br><b style="color: blue;">Bin</b>: '+binval, location=loc)
elif is_bin:
view.show_popup('<b style="color: red;">Dec:</b> '+decval+'<br><b style="color: green;">Hex</b>: '+hexval+'<br><b style="color: blue;">Bin</b>: '+binval, location=loc)
else:
view.show_popup('<b style="color: red;">Hex:</b> '+hexval+'<br><b style="color: blue;">Bin</b>: '+binval, location=loc)
class EventListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
view.run_command('hex_bin_dec_converter', {"from_event": True})
def on_hover(self, view, point, hover_zone):
if hover_zone == sublime.HOVER_TEXT:
view.run_command('hex_bin_dec_converter', {"from_event": True, "show_all": True, "point": point})