【问题标题】:How to convert a txt file from UTF-8 to UTF-16?如何将 txt 文件从 UTF-8 转换为 UTF-16?
【发布时间】:2011-01-28 05:56:45
【问题描述】:

是否有任何脚本(批处理脚本或 ruby​​)将 txt 文件编码从 UTF-8 更改为 UTF-16?

喜欢下面的代码

需要'iconv'

$RegisterMark=[174].pack('U*')

输入'--------'

输入“$RegisterMark Vaule:”

投入$RegisterMark

输入 $RegisterMark.encoding

$utf16RegisterMark = Iconv.conv('UTF-16', 'UTF-8', $RegisterMark )

输入'--------'

输入“$utf16RegisterMark Vaule:”

输入 $utf16RegisterMark

输入 $utf16RegisterMark.encoding

file = File.new("C:/Output.txt","w")

file.puts $utf16RegisterMark

file.close

//////////////////////////

问题:

  1. 为什么 $utf16RegisterMark.encoding 是 ASCII-8BIT 而不是 UTF-16?
  2. 用记事本打开output.txt,检查编码是否为“Unicode Big endian”,如何让其编码为Unicode(Unicode litte endian) ?

【问题讨论】:

  • 嗯... UTF-8 Unicode,当然。
  • 简单地说,Unicode 为每个字符提供了一个数字(称为代码点)。如何存储这个数字称为编码,UTF-8 就是这样。所以你的问题没有意义,对不起。
  • 记事本支持ANSI、Unicode、Unicode big endian、UTF-8,所以Unicode是UTF-16
  • 那么 UTF-16 就是 UTF-16 和 UTF-8 就是 UTF-8 并且都是 Unicode。您应该将您的问题更新为您的真正意思。
  • 在这种情况下,当记事本说“Unicode”时,它的意思是“UTF-16”。 UTF-8 和 UTF-16 都是 Unicode 编码。将术语使用不当归咎于记事本。

标签: ruby utf-8 utf-16


【解决方案1】:
def f x
  x
end

更新:

好的,最初的问题没有提到 UTF-16,只是说“如何将 UTF-8 转换为 Unicode?”。所以,该死的,现在我必须思考并更新一个真实的答案......

irb(main):002:0> require 'iconv'
=> true
irb(main):003:0> Iconv.conv('UTF-16', 'UTF-8', 'How now brown cow')
=> "\376\377\000H\000o\000w\000 \000n\000o\000w\000 \000b\000r\000o\000w\000n\000 \000c\000o\000w"

【讨论】:

    【解决方案2】:

    其实 UTF-8 是 Unicode 的一种形式。

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2015-09-19
      • 2012-06-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      相关资源
      最近更新 更多