"Number Converter" by simon

Rating: (0 votes)

Report this Script

Name: Number Converter
Uploaded By: simon
Date Created: May 2, 2009 3:31 a.m.
Date Modified: May 2, 2009 3:31 a.m.
Description: This script converts the number you have selected in the editor into Decimal, Hexadecimal, Octal and Binary and shows the results in the output window.
Num Views: 380
Num Downloads: 147

Contents

Download this script

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))

Comments: