Tapatalk

Convert UTF-8 to UTF-16

Convert UTF-8 to UTF-16

74
Advanced UserAdvanced User
74

    Feb 01, 2022#1

    I know there's a script to convert files to UTF-8 but I need a script to convert UTF-8 to UTF-16. Can anyone please assist me on this?

    Thank you,
    Max

    6,685587
    Grand MasterGrand Master
    6,685587

      Feb 01, 2022#2

      Code: Select all

      if (UltraEdit.activeDocument.encoding != 1200)  // Is the active file not UTF-16 LE encoded?
      {                                               // That works for ANSI and UTF-8 encoded files
         UltraEdit.activeDocument.ASCIIToUnicode();   // despite the string ASCII in function name.
      }
      // It does not work for UTF-16 BE encoded Unicode files (encoding number 1201). The
      // function ASCIIToUnicode() does not make any change in this case on active file.
      
      Best regards from an UC/UE/UES for Windows user from Austria

      74
      Advanced UserAdvanced User
      74

        Feb 01, 2022#3

        Mofi is there any way to convert multiple of files using a find in files?

        6,685587
        Grand MasterGrand Master
        6,685587

          Feb 01, 2022#4

          I recommend to use the function GetListOfFiles in a script like:

          Code: Select all

          // Include the function GetListOfFiles() here without the code demonstrating the usage.
          
          // Replace the initial directory path in the function parameter list with
          // real path of the directory containing the files to modify by the script.
          if (GetListOfFiles(0,"C:\\Directory\\Path\\","*.*",true))
          {
             // If there was no error and files are found in the directory, the
             // function made the search results output with the file names active.
             // Select all lines in the file and load the file names into an array.
             UltraEdit.activeDocument.selectAll();
             var sLineTerm = "\r\n";   // Default line terminator type is DOS.
             var nLineTermPos = UltraEdit.activeDocument.selection.search(/\r\n|\n|\r/);
             if (nLineTermPos >= 0)    // Any line terminator found?
             {
                // The list file is a Unix file if first character found is a line-feed.
                if (UltraEdit.activeDocument.selection[nLineTermPos] == '\n') sLineTerm = "\n";
                // The list file is a Mac file if first character found is a carriage
                // return and the next character is not a line-feed as in a DOS file.
                else if (UltraEdit.activeDocument.selection[nLineTermPos+1] != '\n') sLineTerm = "\r";
             }
             var asFileNames = UltraEdit.activeDocument.selection.split(sLineTerm);
          
             // The list is not needed anymore and therefore the results window is closed.
             UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
             asFileNames.pop();  // Remove empty string at end of the list.
             
             // Now open one file after the other, process it, save and close the file.   
             // No file in the list should be opened already on script start! Why?
             // http://forums.ultraedit.com/viewtopic.php?f=52&t=4596#p26710
             // contains the answer on point 7 and the solution to enhance
             // this script further if this is necessary in some cases.
             for (var nFileIndex = 0; nFileIndex < asFileNames.length; nFileIndex++)
             {
                UltraEdit.open(asFileNames[nFileIndex]);
          
                if (UltraEdit.activeDocument.encoding != 1200)  // Is the active file not UTF-16 LE encoded?
                {                                               // That works for ANSI and UTF-8 encoded files
                   UltraEdit.activeDocument.ASCIIToUnicode();   // despite the string ASCII in function name.
                   // It does not work for UTF-16 BE encoded Unicode files (encoding number 1201). The
                   // function ASCIIToUnicode() does not make any change in this case on active file.
                   UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
                }
                else UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
             }
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          74
          Advanced UserAdvanced User
          74

            Feb 02, 2022#5

            Thank you Mofi.