Ruby:如何将文件保存为 UTF-16 Little Endian
我想将®保存为UTF-16 Little Endian的txt文件,我在一些方面进行了测试
1.下面的编码是UTF-8
$RegisterMark=[174].pack('U*') file = File.new("C:/Output.txt","w") 文件.puts $RegisterMark 文件.关闭
2.下面的编码是UTF-16 Big Endian
需要“iconv” $RegisterMark=[174].pack('U*') $utf16RegisterMark =Iconv.conv('UTF-16', 'UTF-8', $RegisterMark ) file = File.new("C:/Output.txt","w") file.puts $utf16RegisterMark 文件.关闭
mentod Iconv.conv 不支持 UTF-16 LE 类型。
如何使用 UTF16 LE 保存 output.txt?
I want to save ® into a txt file with UTF-16 Little Endian, I tested in some ways
1.The encoding below is UTF-8
$RegisterMark=[174].pack('U*') file = File.new("C:/Output.txt","w") file.puts $RegisterMark file.close
2.The encoding below is UTF-16 Big Endian
require 'iconv' $RegisterMark=[174].pack('U*') $utf16RegisterMark =Iconv.conv('UTF-16', 'UTF-8', $RegisterMark ) file = File.new("C:/Output.txt","w") file.puts $utf16RegisterMark file.close
The mentod Iconv.conv doesn't suport UTF-16 LE type.
How can I save output.txt with UTF16 LE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是首先以 UTF-16LE 格式打开文件:
这里重要的一点是使用
选项中的
File.new 方法的:encoding
键显式指定文件的编码Hash
(或者在本例中为File.open
)。这样,写入文件的字符串将自动转换,无论它们采用什么编码。我还冒昧地将代码更改为更惯用的 Ruby 风格:
snake_case
,变量名和方法名不是驼峰命名法。File.open
的块形式,即使出现错误或异常,它也会为您关闭文件。t
修饰符。它在大多数操作系统上没有任何区别(不幸的是,这就是为什么大多数 Rubyists 忘记传递它的原因),但它在 Windows 上至关重要,这就是您似乎正在使用的操作系统。The easiest way is to just open the file as UTF-16LE in the first place:
The important bit here is to explicitly specify the encoding of the file, using the
:encoding
key in theoptions
Hash
of theFile.new
method (or in this case,File.open
). That way, strings written to the file will be converted automatically, no matter what encoding they are in.I also took the liberty of changing your code to a more idiomatic Ruby style:
snake_case
, notCamelCase
for variable and method names.Array#pack
here, just write down what you want.File.open
, which will take care of closing the file for you, even in the case of an error or exception.t
modifier. It doesn't make any difference on most operating systems (which is why, unfortunately, most Rubyists forget to pass it), but it is crucial on Windows, which is what you appear to be using.有点老套,但这对我有用。具体来说,我试图让 ruby 输出带 BOM 的 UTF-16LE
Somewhat hacky, but this worked for me. Specifically, I was trying to get ruby to output UTF-16LE w/ BOM