Hex to Dec, How?

Hex to Dec, How?

5
NewbieNewbie
5

    Apr 07, 2008#1

    Is there some function to convert hex -> dec?

    6,686585
    Grand MasterGrand Master
    6,686585

      Apr 08, 2008#2

      No, not built-in in UltraEdit! You can write a script which makes that conversion for the selected text or if nothing is selected asks the user for a hex value, converts it to decimal and writes it into the file at current cursor position (if there is a file open) or to the output window or shows the decimal value with a message box.

      Hm, that could be useful for me and I guess for many other users too and would be a script worth to send it to IDM for the Macros & Scripts download area. Problem is only that I have currently no time to develop this script. If somebody else wants to start with it, feel free to do so.

      Just a few notes: A selected text or entered string in the input dialog is a valid hex value if it starts with "0x" or "0X" (programming languages like C/C++, ...) or if it ends with an 'h' or 'H' (assemblers) or the rest of the text consists only of the characters [0-9a-fA-F]. Where to output the decimal value (into current file, output window or message box) should be an optional parameter on function call with a default setting if not specified on function call. Another optional parameter which could be required is if the hex value should be treated unsigned or signed. If signed and highest hex byte is greater '7', the decimal value is negative. And the length of the hex value should be also checked before making the conversion. It would not be a good idea to try to convert a string like "FF9032AB32098". I think a maximum length of 8 hex characters (without 0x or H) for 32 bit values should be enough. Maybe optional also 64 bit, if the script engine can handle 64 bit values.

      262
      MasterMaster
      262

        Apr 08, 2008#3

        No time either for writing a fully fledged Hex-to-Dec converter.

        But if cosco needs something quick to work with, have a look at this:

        Code: Select all

        if (UltraEdit.activeDocument.isSel()) { /* is anything selected ? */
          var isThisHex = UltraEdit.activeDocument.selection; /* get selected text */
          try {
            var decNumber = parseInt( isThisHex, 16 ); /* Try and convert with standard javascript parseInt */
            if(String(decNumber)=="NaN") {
              /* do nothing if it can't be converted - Insert your own error handling */
            }
            else {
              UltraEdit.activeDocument.write(String(decNumber)); /* write the dec number back into active document */
            }
          }
          catch (conversionError) {
            /* do nothing if it can't be converted - Insert your own error handling */
          }
        }
        // When selected the hex below will be converted into:
        // FFFF   -> 65535
        // 0xFFFF -> 65535