import pn, scintilla def hex2dec(s): """return the integer value of a hexadecimal string s""" return int(s, 16) def Denary2Binary(n): """convert denary integer n to binary string bStr""" bStr = '' if n < 0: raise ValueError, "must be a positive integer" if n == 0: return '0' while n > 0: bStr = str(n % 2) + bStr n = n >> 1 return bStr @script("ConvertNumber") def ConvertNumber(): s = scintilla.Scintilla(pn.CurrentDoc()) if s.SelectionEnd - s.SelectionStart < 1: return sel = s.SelText if sel.find('0x') != -1: sel = sel.replace("0x", "") sel = hex2dec(sel) else: sel = int(sel) pn.AddOutput("Dec: %d\n" % sel) pn.AddOutput("Hex: 0x%X\n" % sel) pn.AddOutput("Oct: %o\n" % sel) pn.AddOutput("Bin: %s" % Denary2Binary(sel))