UTF-16 到 UTF-8 的转换(用于 Windows 中的脚本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/265370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:40:17  来源:igfitidea点击:

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

windowsutf-8batch-filecmdutf-16

提问by Grzenio

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

将 UTF-16 文件转换为 UTF-8 的最佳方法是什么?我需要在 cmd 脚本中使用它。

回答by Kaarel

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

有一个 GNU 工具重新编码,您也可以在 Windows 上使用它。例如

recode utf16..utf8 text.txt

回答by Jon Skeet

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...

Ruby 的替代方法是用 C# 编写一个小的 .NET 程序(.NET 1.0 会很好,虽然 2.0 会更简单:) - 这是一段非常简单的代码。您是否希望在没有任何其他应用程序的情况下做到这一点?如果你想要一些代码来做到这一点,请添加评论,我会填写答案......

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);
    }
  }
}

回答by Tor Haugen

Certainly, the easiestway 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..

当然,最简单的方法是将脚本加载到记事本中,然后使用 UTF-8 编码再次保存。这是另存为对话框中的一个选项。

回答by PhiLho

Perhaps with iconv?

也许用iconv

回答by VonC

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

如果您安装了 ruby​​ 发行版,您可以调用一个 ruby​​ 脚本来处理转换:

Ruby script to convert file(s) character encoding

用于转换文件字符编码的 Ruby 脚本

In the same spirit: Perl script

本着同样的精神:Perl 脚本

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

在没有脚本支持的情况下,您必须使用 WideCharToMultiByte() 调用像这个C++ 源代码一样对其进行编码...

回答by Ben Collins

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

您可以使用内置的 PowerShell cmdlet 轻松完成此操作,您可以从 cmd 调用它:

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