Community
    • Login

    Number conversion feature or plugin

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    13 Posts 4 Posters 8.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mylittleplaceholderM
      mylittleplaceholder
      last edited by

      Hi,

      I’m looking for a tool to convert numbers to different bases. I do use Converter to switch between raw binary data and hex strings, but it doesn’t convert to other number bases.

      For example, I can convert Test123 to 54 65 73 74 31 32 33 with ASCII -> HEX but I’d like to be able to convert to other number formats, such as hex to binary text or decimal. Ideally, converting runs of numbers to another format.

      I used to use TextFX to convert numbers but it’s deprecated and doesn’t handle sequences of numbers as well.

      Is there anything available to convert number bases?

      If not, is there an support to send to another script, such as
      perl -nE '@x=split; foreach (@x) {say hex;}'
      to convert from decimal to hex?

      Thanks.

      William-AbbotW PeterJonesP 2 Replies Last reply Reply Quote 1
      • William-AbbotW
        William-Abbot @mylittleplaceholder
        last edited by William-Abbot

        @mylittleplaceholder In the drop down menu for the converter plugin, there is a “Conversion Panel” command. It pulls up a window that you can use to convert between number bases. Is that what you are looking for?

        mylittleplaceholderM 1 Reply Last reply Reply Quote 0
        • PeterJonesP
          PeterJones @mylittleplaceholder
          last edited by

          @mylittleplaceholder ,

          If you have perl on your windows system (I do), you could use Notepad++'s Run > Run… menu command: cmd /s /c "perl -E "say hex for @ARGV" $(CURRENT_WORD) | clip"

          Make your selection of the numbers you want to change, run that command, and the converted numbers will be in your clipboard.

          The online usermanual has a section on the $(CURRENT_WORD) and similar pseudo-variables for Run commands

          mylittleplaceholderM 1 Reply Last reply Reply Quote 2
          • mylittleplaceholderM
            mylittleplaceholder @William-Abbot
            last edited by

            @William-Abbot The conversion panel for me has ASCII and Decimal and just converts back and forth between a single ASCII character and its ASC value. I have NPP Converter v4.4. Does a different version have other number bases or is there a different converter?

            Thank you.

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @mylittleplaceholder
              last edited by PeterJones

              @mylittleplaceholder

              58c8a91d-3272-46d6-a2b6-84205f965700-image.png

              37bf2213-ca5b-4530-9017-3026af224dd6-image.png

              mylittleplaceholderM 1 Reply Last reply Reply Quote 1
              • mylittleplaceholderM
                mylittleplaceholder @PeterJones
                last edited by

                @PeterJones Thank you, that’s very helpful. I run perl under Cygwin, but I can probably get that to work or install a native exe.

                Is there a way to define different named and predefined “run” functions under a menu, so I could do “convert to decimal,” “convert to hex,” etc.? Also is it possible to paste it in automatically to replace the selection (not that hitting ^V is all that hard, just wondering).

                Thanks.

                1 Reply Last reply Reply Quote 0
                • mylittleplaceholderM
                  mylittleplaceholder @PeterJones
                  last edited by

                  Haha, my popup window for that opens as a small area and I never thought to stretch it open! It didn’t look resizable to me. Thanks.

                  conversion.png

                  That will help some, though it doesn’t help with a string of characters of numbers, which is usually what I’m doing.

                  Alan KilbornA 1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @mylittleplaceholder
                    last edited by PeterJones

                    @mylittleplaceholder

                    If you give a real solid example of what you want to do, it can be scripted more formally in Notepad++ with a scripting plugin. Pretty much anything would be possible, overcoming all of the limitations you’ve discussed.

                    mylittleplaceholderM 1 Reply Last reply Reply Quote 0
                    • mylittleplaceholderM
                      mylittleplaceholder @Alan Kilborn
                      last edited by

                      @Alan-Kilborn Thank you.

                      I usually use these conversions when reverse engineering malicious emails or broken websites and related things when I don’t want to drop to writing a script to analyze it. Most of the functions (e.g., b64, html, or URL encoding/decoding) are in Notepad++, so I can get pretty far with deobfuscating them just by looking at the source there.

                      An example is a script containing:
                      60, 97, 32, 104, 114, 101, 102

                      I can convert that to hex and then ASCII. Converting directly to ASCII would be helpful, but really just converting it to hex and back would really be all I’d need.

                      3C 61 20 68 72 65 66
                      <a href

                      A technique I’ve used in the past is use a regex to convert to HTML entities
                      <a href
                      and then use TextFX “Strip HTML Tags” to convert to ASCII.

                      It’s just faster than trying to reverse it in JS or Perl.

                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @mylittleplaceholder
                        last edited by Alan Kilborn

                        @mylittleplaceholder

                        So here’s a little PythonScript demo’ing some capability:

                        # -*- coding: utf-8 -*-
                        from __future__ import print_function
                        
                        # references:
                        #  https://community.notepad-plus-plus.org/topic/23341/number-conversion-feature-or-plugin
                        
                        from Npp import *
                        import inspect
                        import os
                        
                        #-------------------------------------------------------------------------------
                        
                        class T23341(object):
                        
                            def __init__(self):
                                self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
                                sel_start = editor.getSelectionStart()
                                sel_end = editor.getSelectionEnd()
                                if sel_start != sel_end:
                                    while True:
                                        user_input = self.prompt('Enter D if numbers are currently in decimal; H if hex')
                                        if user_input == None: return  # user cancel
                                        if user_input in [ 'H', 'h' ]:
                                            self.to_hex = False
                                            break
                                        if user_input in [ 'D', 'd' ]:
                                            self.to_hex = True
                                            break
                                    doc_len_start = editor.getLength()
                                    editor.rereplace(r'\b[0-9]+\b' if self.to_hex else r'\b[0-9A-F]+\b', lambda m: self.replace_function(m), 0, sel_start, sel_end)
                                    len_delta = editor.getLength() - doc_len_start
                                    editor.setSelection(sel_end + len_delta, sel_start)
                        
                            def replace_function(self, m):
                                fmt = '{:X}' if self.to_hex else '{}'
                                return fmt.format(int(m.group(0), 10 if self.to_hex else 16))
                        
                            def prompt(self, prompt_text, default_text=''):
                                if '\n' not in prompt_text: prompt_text = '\r\n' + prompt_text
                                prompt_text += ':'
                                return notepad.prompt(prompt_text, self.this_script_name, default_text)
                        
                        #-------------------------------------------------------------------------------
                        
                        if __name__ == '__main__': T23341()
                        

                        Before running the script, select some text, maybe like so:

                        2d232801-23d5-46ef-8f65-ca577100b79c-image.png

                        and then run the script and it will prompt you thusly:

                        6592fefe-e7e6-4c53-a5c1-0dda2743103c-image.png

                        Type D (or d works too) and click OK and it will change the selected text to:

                        36bd902b-fd7b-4ebc-944b-811e05dd7909-image.png

                        Alan KilbornA mylittleplaceholderM 2 Replies Last reply Reply Quote 3
                        • Alan KilbornA Alan Kilborn referenced this topic on
                        • Alan KilbornA
                          Alan Kilborn @Alan Kilborn
                          last edited by

                          and then run the script and it will prompt you…

                          If you’re wondering why the prompting is needed, let me point out that all of the numbers in 60, 97, 32, ... are equally valid decimal OR hexadecimal, so the script has to be told what to do.

                          mylittleplaceholderM 1 Reply Last reply Reply Quote 0
                          • mylittleplaceholderM
                            mylittleplaceholder @Alan Kilborn
                            last edited by

                            @Alan-Kilborn Pretty nice. Thanks for working on this. That will be helpful.

                            1 Reply Last reply Reply Quote 1
                            • mylittleplaceholderM
                              mylittleplaceholder @Alan Kilborn
                              last edited by

                              @Alan-Kilborn I think I’ll duplicate it and make “hex to decimal.py” and “decimal to hex.py” to skip the dialog. Very helpful.

                              1 Reply Last reply Reply Quote 2
                              • First post
                                Last post
                              The Community of users of the Notepad++ text editor.
                              Powered by NodeBB | Contributors