How to copy hex value?

How to copy hex value?

5
NewbieNewbie
5

    Apr 07, 2008#1

    How do I copy the hex value of the character at current caret position?

    262
    MasterMaster
    262

      Apr 08, 2008#2

      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

      This command has by default the hotkey: Alt+RETURN

      5
      NewbieNewbie
      5

        Apr 08, 2008#3

        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")

        262
        MasterMaster
        262

          Apr 11, 2008#4

          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.

          6,680583
          Grand MasterGrand Master
          6,680583

            Jan 10, 2010#5

            I wrote a script containing the function HexCopy, see Macros & Scripts page. 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 for Windows / UEStudio and executed using Scripting - Run active script (traditional menus) or Advanced - Play script (ribbon mode) 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.

            The line and block comments can be removed from entire script or function HexCopy by running a replace all (from top of file) searching with Perl regular expression for ^ *//.+[\r\n]+|^ */\*[\s\S]+?\*/[\r\n]+| +//.+$ and using an empty replace string. The first part in this OR expression with three arguments matches entire lines containing only a line comment, the second part matches block comments, and third part matches line comments right to code. Removal of the comments makes the usage of this script or just the function in other scripts more efficient because of JavaScript interpreter has to interpret less characters and lines.

            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.

            2
            NewbieNewbie
            2

              Feb 15, 2023#6

              I used your HexCopy script and now I have a problem.

              I opened a binary file (a non-text file) in UltraEdit's hexadecimal mode and wanted to copy its hexadecimal data. I used your HexCopy script function to copy to the Windows clipboard, and the copy was interrupted when 0x00 was encountered. What might be the reason for this?

              Thank you very much.

              6,680583
              Grand MasterGrand Master
              6,680583

                Feb 15, 2023#7

                You are unfortunately right. The script function HexCopy works fine as designed more than 12 years ago with UltraEdit for Windows up to version 23.20.0.34. But it does not work anymore since UltraEdit for Windows version 23.20.0.40 released 2016-09-30 according to my records as I found out today. There are just the bytes up to first null byte processed and the hexadecimal data of all bytes up to first null byte is also incorrect.

                I can only suggest to use the command Hex copy selected view, paste the data in clipboard and reformat them by yourself to the wanted format if you don't need the function HexCopy for usage in an UltraEdit script.

                The command for copying a selection in hex edit mode as displayed by UltraEdit can be executed with
                • clicking in ribbon mode on tab Edit in last group Hex mode on the command Copy selected;
                • clicking in toolbar/menu mode with contemporary menus in menu Edit on last menu item Copy selected;
                • clicking in toolbar/menu mode with traditional menus in menu Edit in submenu HEX functions on last menu item Hex copy selected view.
                It looks like UltraEdit for Windows interprets the binary bytes in active file or in active clipboard since v23.20.0.40 as an array of characters with one byte per character terminated with a null byte and converts the "characters" up to first null byte to a UTF-16 encoded string for UltraEdit.clipboardContent or UltraEdit.activeDocument.selection which are both JavaScript String objects. I have no idea how to get access to raw binary data stream in clipboard or in active file from within the UltraEdit script environment. In former versions of UltraEdit the binary bytes are copied to UltraEdit.clipboardContent or UltraEdit.activeDocument.selection as is without any additional interpreting of the bytes as characters.

                I asked UltraEdit support about this change in interpreting binary data in clipboard and in a (binary) file opened in hex edit mode. There is no possibility since UltraEdit for Windows v23.20.0.40 for getting in an UltraEdit script access to the binary bytes in active clipboard or active file. I requested therefore an enhancement in scripting environment for properties like UltraEdit.binaryClipboardContent and UltraEdit.activeDocument.binarySelection for working with the binary data in an UltraEdit script.
                Best regards from an UC/UE/UES for Windows user from Austria

                2
                NewbieNewbie
                2

                  Feb 26, 2023#8

                  Thank you very much for your detailed answer! I found an older version of the UltraEdit program to use. The HexCopy function is ready to run. Looking forward to your good news.