Simple Decimal To Hex script

Simple Decimal To Hex script

2
NewbieNewbie
2

    Jul 19, 2011#1

    I need help editing a script to convert decimal numbers to hex.
    I found an existing script on another thread but its for the reverse hex to dec. See here: Hex to Dec, How?

    However I would like it if it could covert the whole file not just a selected string.

    6,682583
    Grand MasterGrand Master
    6,682583

      Jul 19, 2011#2

      Converting all hexadecimal numbers in a file to decimal is not really difficult.

      Code: Select all

      if (UltraEdit.document.length > 0) {
      
         // Define environment for script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.perlReOn();
      
         // To find the hexadecimal numbers, a not case sensitive Perl
         // regular expression find from top to bottom of file is used.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.top();
      
         // Find hexadecimal numbers with "0x" or without "0x" and convert to decimal.
         // Numbers existing only of digits are also interpreted as hexadecimal numbers.
         while (UltraEdit.activeDocument.findReplace.find("\\<(?:0x)*[0-9A-F]+\\>")) {
      
            var sHexNum = UltraEdit.activeDocument.selection.replace(/0x/g,"");
            var nDecNum = parseInt(sHexNum,16);
            if (!isNaN(nDecNum)) {  // It is very unlikely that conversion failed.
               // Overwrite still selected hexadecimal number by decimal number.
               UltraEdit.activeDocument.write(nDecNum.toString());
            }
         }
         UltraEdit.activeDocument.top();
      }
      Converting all decimal numbers in a file to hexadecimal is also quite simple.

      Code: Select all

      if (UltraEdit.document.length > 0) {
      
         // Define environment for script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.perlReOn();
      
         // To find the decimal numbers, a case sensitive (faster) Perl
         // regular expression find from top to bottom of file is used.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.top();
      
         // Find decimal numbers and convert to hexadecimal.
         while (UltraEdit.activeDocument.findReplace.find("\\<[0-9]+\\>")) {
      
            var nDecNum = parseInt(UltraEdit.activeDocument.selection,10);
            if (!isNaN(nDecNum)) {  // It is very unlikely that conversion failed.
               // Convert the decimal number to (lowercase) hexadecimal.
               var sHexNum = nDecNum.toString(16);
               // For a fixed length of the hexadecimal numbers, uncomment the
               // following line and define the minimum width for the number.
               // while (sHexNum.length < 4) sHexNum = '0' + sHexNum;
               // Overwrite still selected decimal number by hexadecimal number.
               UltraEdit.activeDocument.write(sHexNum.toUpperCase());
            }
         }
         UltraEdit.activeDocument.top();
      }

      2
      NewbieNewbie
      2

        Jul 19, 2011#3

        Awesome! This works great, thanks!