Simple Decimal To Hex script

Help with writing and running scripts

Simple Decimal To Hex script

Postby Neldo » Mon Jul 18, 2011 8:02 pm

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.
Neldo
Newbie
 
Posts: 2
Joined: Mon Jul 18, 2011 7:49 pm

Re: Simple Decimal To Hex script

Postby Mofi » Tue Jul 19, 2011 12:19 am

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();
   UltraEdit.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();
   UltraEdit.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();
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Simple Decimal To Hex script

Postby Neldo » Tue Jul 19, 2011 1:56 pm

Awesome! This works great, thanks!
Neldo
Newbie
 
Posts: 2
Joined: Mon Jul 18, 2011 7:49 pm


Return to Scripts