Reputation: 31
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?
Upvotes: 3
Views: 5476
Reputation: 342
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')
Upvotes: 2
Reputation: 369633
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:
snake_case
, not CamelCase
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.Upvotes: 7