Ruby:如何将文件保存为 UTF-16 Little Endian

发布于 2024-10-15 02:43:52 字数 572 浏览 1 评论 0原文

我想将®保存为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2024-10-22 02:43:52

最简单的方法是首先以 UTF-16LE 格式打开文件:

register_mark = "\00ua3" # or even just: register_mark = ®

File.open('C:/Output.txt', 'wt', encoding: 'UTF-16LE') do |f|
  f.puts register_mark
end

这里重要的一点是使用 选项中的 :encoding 键显式指定文件的编码 File.new 方法的 Hash(或者在本例中为 File.open)。这样,写入文件的字符串将自动转换,无论它们采用什么编码。

我还冒昧地将代码更改为更惯用的 Ruby 风格:

  • Ruby 社区使用 snake_case,变量名和方法名不是驼峰命名法。
  • 应避免全局变量,特别是因为在您的示例中,它们无论如何都是完全多余的。
  • 这里其实没必要用Array#pack,写下你想要的就可以了。
  • 只要有可能,请使用 File.open 的块形式,即使出现错误或异常,它也会为您关闭文件。
  • 处理文本文件时,应始终传递 t 修饰符。它在大多数操作系统上没有任何区别(不幸的是,这就是为什么大多数 Rubyists 忘记传递它的原因),但它在 Windows 上至关重要,这就是您似乎正在使用的操作系统。

The easiest way is to just open the file as UTF-16LE in the first place:

register_mark = "\00ua3" # or even just: register_mark = ®

File.open('C:/Output.txt', 'wt', encoding: 'UTF-16LE') do |f|
  f.puts register_mark
end

The important bit here is to explicitly specify the encoding of the file, using the :encoding key in the options Hash of the File.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:

  • The Ruby community uses snake_case, not CamelCase for variable and method names.
  • Global variables should be avoided, especially since in your example, they are totally superfluous anyway.
  • There's really no need to use Array#pack here, just write down what you want.
  • Whenever possible, use the block form of File.open, which will take care of closing the file for you, even in the case of an error or exception.
  • When dealing with text files, you should always pass the 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.
倚栏听风 2024-10-22 02:43:52

有点老套,但这对我有用。具体来说,我试图让 ruby​​ 输出带 BOM 的 UTF-16LE

## Adds BOM, albeit in a somewhat hacky way.
new_html_file = File.open(foo.txt, "w:UTF-8")
new_html_file << "\xFF\xFE".force_encoding('utf-16le') + some_text.force_encoding('utf-8').encode('utf-16le')

Somewhat hacky, but this worked for me. Specifically, I was trying to get ruby to output UTF-16LE w/ BOM

## Adds BOM, albeit in a somewhat hacky way.
new_html_file = File.open(foo.txt, "w:UTF-8")
new_html_file << "\xFF\xFE".force_encoding('utf-16le') + some_text.force_encoding('utf-8').encode('utf-16le')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文