"Hex Dump" by NickDMax

Rating: (0 votes)

Report this Script

Name: Hex Dump
Uploaded By: NickDMax
Date Created: May 18, 2009 1:25 a.m.
Date Modified: May 18, 2009 1:25 a.m.
Description: Will extract the current selection as a hex dump. It is still pretty rough but it has come in handy for me so I thought I would share.
Num Views: 384
Num Downloads: 163

Contents

Download this script

###############################################################################
## Doc2Hex v0.1 -- Will extract the current selection into a new document
##    Formatting the text as a Hex Dump. 
## By: NickDMax

import pn
import scintilla
from pypn.decorators import script

def HexEncode(text):
    output = ""
    if text is not None:
        lineLength = 0
        position = 0
        while len(text) > 0:
            output += "%08X |" % position
            if len(text) <= 16:
                lineLength = len(text)
            else:
                lineLength = 16
            snippet = text[0: lineLength]
            for x in snippet:
                output += " %02X" % ord(x)
            output += "\n"
            position += 16;
            text = text[lineLength:]
    return output

@script("Doc2Hex", "DocUtils")
def Doc2Hex():
    """ Doc2Hex will convert the current document into a HexDump """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(pn.CurrentDoc())
        start = editor.SelectionStart
        end = editor.SelectionEnd
        if (start == end):  #nothing is selected so we will just grab it all...
            start = 0
            end = editor.Length 
        text = editor.GetTextRange(start, end)
        newDoc = pn.NewDocument(None)
        newEditor = scintilla.Scintilla(newDoc)
        newEditor.BeginUndoAction()
        encoded = HexEncode(text)
        newEditor.AppendText(len(encoded), encoded)        
        newEditor.EndUndoAction()

Comments: