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