How copy hex value?

Help with writing and running scripts

How copy hex value?

Postby cosco » Mon Apr 07, 2008 6:49 pm

How I copy the hex value of the present position?
cosco
Newbie
 
Posts: 5
Joined: Mon Apr 07, 2008 10:38 am

Re: How copy hex value?

Postby jorrasdk » Tue Apr 08, 2008 4:36 am

Do you just need to know the hex value or do you need it on the clipboard or ?

If you only need to know the hex value use:
Search - Character Properties

which has a default hoy key as Alt+RETURN
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: How copy hex value?

Postby cosco » Tue Apr 08, 2008 7:30 am

I need the hex value on the clipboard.
First I will find the string "RGCN" in an archive and after I want copy the hex values of the next four byte:

Example:

52 47 43 4E 52 41 48 43
R G C N R A H C

I want copy on clipboard the string "52414843" (the hex value of the next four byte after of the string "RGCN")
cosco
Newbie
 
Posts: 5
Joined: Mon Apr 07, 2008 10:38 am

Re: How copy hex value?

Postby jorrasdk » Fri Apr 11, 2008 5:03 am

Ok, below is a script that will solve this task. Comments are in the script itself. Use UE help to learn about the script commands. Adjust the script to fit your purpose.

Code: Select all
// RGCNRAHC
//
if (UltraEdit.outputWindow.visible == false) { // open output window
  UltraEdit.outputWindow.showWindow(true);
}
UltraEdit.outputWindow.clear(); // clear output window

var findString = UltraEdit.getString("String to find",1); // Get string to find

findString = findString + "(....)"; // Add a regular expression to capture next 4 characters

UltraEdit.activeDocument.top(); // go to top of active document
UltraEdit.perlReOn(); // Use Perl reg exp
UltraEdit.activeDocument.findReplace.regExp=true; // Use regExp for search

if(UltraEdit.activeDocument.findReplace.find(findString)) { // search for string, enter if, if found
  var foundValue = UltraEdit.activeDocument.selection; // Get selected text

  var foundChars = (new RegExp(findString)).exec(foundValue); // Re-execute regExp on found value to extract 4 bytes
  var foundChars = foundChars[1]; // Get the tagged expression of 4 chars.
  var hexChars   = chr2hex(foundChars); // Convert from string to hex using chr2hex

  UltraEdit.outputWindow.write("Searching for...........: " + findString);
  UltraEdit.outputWindow.write("Chars to be hex escaped.: " + foundChars);
  UltraEdit.outputWindow.write("Hex value...............: " + hexChars);   // Write result into output window

  // Now open a blank doc and cut the hexChars into the active clipboard
  // You could write it into the active file, but sometimes you don't want the "changed attribute" to be set.
  var activeDocIx = getActiveDocumentIndex(); // to be able to restore focus on active document
  UltraEdit.newFile();
  UltraEdit.activeDocument.write(hexChars);
  UltraEdit.activeDocument.selectToTop();
  UltraEdit.activeDocument.cut();
  UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
  UltraEdit.document[activeDocIx].setActive(); // restore focus
  // now the hex value is on the clipboard

}

UltraEdit.outputWindow.showStatus=false;

function chr2hex(chrs) {
  var hexDigits = '0123456789ABCDEF';
  if((String(chrs).length==0) || (chrs==null)) {
     return "";
  }
 
  var hexChars = "";
  for(var i=0;i<String(chrs).length;i++) { // iterate depending on length of chrs
    var dec = String(chrs).charCodeAt(i); // First convert to dec.
    hexChars = hexChars + ( hexDigits[ dec >> 4 ] + hexDigits[ dec & 15 ] ); // convert to hex
  }
  return hexChars;
}

function getActiveDocumentIndex() { /* Find the tab index of the active document */
  var tabindex = -1; /* start value */

  for (i = 0; i < UltraEdit.document.length; i++)
  {
    if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
      tabindex = i;
      break;
    }
  }
  return tabindex;
}

When run upon itself the script will write the following in the output window:

Code: Select all
Searching for...........: RGCN(....)
Chars to be hex escaped.: RAHC
Hex value...............: 52414843

and the value "52414843" is on the active clipboard ready for paste.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: How copy hex value?

Postby Mofi » Sun Jan 10, 2010 8:53 am

I wrote a script containing the function HexCopy, see Downloads - Extras - Scripts. The function offers lots of options.

The function can copy/write the output to the active clipboard (copy or append), to a new file (with or without saving the new file), any already opened file or return the output as string to the calling routine.

The input source can be the active clipboard, a selection in the active file or the entire content of active file - binary or text file, or a string passed to the function.

For the output it supports all 3 types of line termination - DOS, UNIX and MAC.

The last line of the output can be with or without a line termination.

The number of hexadecimal values in the ASCII output string can be defined in the range of 1 to 512. More than 512 values per line is possible if you modify the script code. The upper limit of 512 values has no real reason.

The format of the hexadecimal values can be also modified with a parameter.

The function can output also an offset information at start of every line and with a parameter the format of the offset information can be customized.

Also possible is to output an ASCII/ANSI representation of the bytes and this string at end of every line can be customized too.

The script contains lots of comments at top explaining all the options. This description is quite long because of the various formats and options which can be used on calling function HexCopy.

To make it easier for you to get an idea what the function can be used for, the script file (packed with ZIP) can be simply opened in UltraEdit v13.00+ / UEStudio v6.20+ and executed using Scripting - Run Active Script because it contains code at bottom for demonstrating the usage of the function.

Any bug reports or suggestions for further improvements are welcome. Just reply to this topic.

Please note that the usage of some features depends on the version of UE / UES. So read the comments if something is not working.

PS: Originally I just wanted a replacement for command Hex Copy Selected View not available as script or macro command. But the final function HexCopy is much more powerful.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts