How to create a new binary file from a string with hex values in ASCII?

Help with writing and running scripts

How to create a new binary file from a string with hex values in ASCII?

Postby sidewinders » Thu Jul 23, 2009 7:14 am

Hi there,

I have a problem trying to create a binary file using UltraEdit.

For example, I have an ASCII string as follows:
"0123456789abcdef"
and I need to create a binary file that contain the string above.
So the file will contain
0x01 0x23 0x45 0x67 0x89 0xab 0xcd 0xef (in total the size of the file will be 8 bytes).

In fact, I have a string of 1000 hexadecimal bytes and I don't think I want to type it manually using hex edit function.

Any help would be appreciated.

cheers
Rudy
sidewinders
Newbie
 
Posts: 1
Joined: Thu Jul 23, 2009 7:10 am

Re: How to create a new binary file from a string with hex values in ASCII?

Postby Mofi » Thu Jul 23, 2009 9:13 am

Quite simple using a script. Tested on your example and some others with UE v15.10.0.1019. The script expects the string in the current clipboard.

Code: Select all
// This little script writes an ASCII string like
// FFFE540068006900730020006900730020006A00750073007400200061006E0020006500780061006D0070006C0065000D000A006F0066002000610020006D0075006C00740069002D006C0069006E006500200073007400720069006E0067000D000A0065006E0063006F00640065006400200069006E0020005500540046002D003100360020004C0045002E00
// into a new file in hex edit mode. The string must be in the active clipboard.
var sHexValues = UltraEdit.clipboardContent;
var nValueCount = sHexValues.length;
var sErrorText = "";
if (nValueCount > 0) {           // Is clipboard not empty?
   if (!(nValueCount % 2)) {     // Is the number of characters even?
      // Does the string only contain valid hexadecimal characters?
      if (sHexValues.search(/[^0-9a-f]/i) < 0) {
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         UltraEdit.newFile();
         UltraEdit.activeDocument.write("!");
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.hexOn();
         UltraEdit.activeDocument.hexDelete(1);
         UltraEdit.activeDocument.write(sHexValues);
         // The next 2 lines are required to see the binary data correct in hex edit mode.
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.hexOn();
         UltraEdit.activeDocument.top();
         UltraEdit.saveAs("");
      } else sErrorText = "Clipboard contains non hexadecimal characters!";
   } else sErrorText = "Clipboard contains odd number of characters!";
} else sErrorText = "Clipboard is empty!";
if (sErrorText != "") {
   if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
   UltraEdit.outputWindow.write("Error: "+sErrorText);
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts