开发者

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

开发者 https://www.devze.com 2023-02-07 06:01 出处:网络
I wan开发者_开发问答t to save ® into a txt file with UTF-16 Little Endian, I tested in some ways

I wan开发者_开发问答t 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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消