Ruby: how to save file to UTF-16 Little Endian

守給你的承諾、 提交于 2019-12-10 19:23:00

问题


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?


回答1:


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.



回答2:


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')


来源:https://stackoverflow.com/questions/4840630/ruby-how-to-save-file-to-utf-16-little-endian

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