UTF-16 to UTF-8 conversion (for scripting in Windows)

血红的双手。 提交于 2019-11-27 05:58:01

问题


what is the best way to convert a UTF-16 files to UTF-8? I need to use this in a cmd script.


回答1:


There is a GNU tool recode which you can also use on Windows. E.g.

recode utf16..utf8 text.txt



回答2:


An alternative to Ruby would be to write a small .NET program in C# (.NET 1.0 would be fine, although 2.0 would be simpler :) - it's a pretty trivial bit of code. Were you hoping to do it without any other applications at all? If you want a bit of code to do it, add a comment and I'll fill in the answer...

EDIT: Okay, this is without any kind of error checking, but...

using System;
using System.IO;
using System.Text;

class FileConverter
{
  static void Main(string[] args)
  {
    string inputFile = args[0];
    string outputFile = args[1];
    using (StreamReader reader = new StreamReader(inputFile, Encoding.Unicode))
    {
      using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding.UTF8))
      {
        CopyContents(reader, writer);
      }
    }
  }

  static void CopyContents(TextReader input, TextWriter output)
  {
    char[] buffer = new char[8192];
    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) != 0)
    {
      output.Write(buffer, 0, len);
    }
  }
}



回答3:


Certainly, the easiest way is to load the script into notepad, then save it again with the UTF-8 encoding. It's an option in the Save As dialog box..




回答4:


Perhaps with iconv?




回答5:


If you have a ruby distribution installed, you can call a ruby script taking care of the conversion:

Ruby script to convert file(s) character encoding

In the same spirit: Perl script

In the absence of script support, you would have to code it like this C++ source using a WideCharToMultiByte() call...




回答6:


You can do this easily with built-in PowerShell cmdlets, which you can invoke from cmd:

C:\> powershell -c "Get-Content mytext.txt | Set-Content -Encoding utf8 mytext_utf8.txt"


来源:https://stackoverflow.com/questions/265370/utf-16-to-utf-8-conversion-for-scripting-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!