From: "Y. NOBUOKA" Date: 2011-01-13T21:55:43+09:00 Subject: Re: Ruby 1.9.2 encoding issue Please check +File#external_encoding+. When +File#external_encoding+ is not +nil+, +File#write+ method tries to change the encoding of the written string into +File#external_encoding+. If the written data is binary string which contains 0x80 .. 0xFF and +File#external_encoding+ is "UTF-8", +File#write+ method tries to encode the data, but it can't!! So an exception will be raised. Please see following example. binary_data = [ 0x80, 0x81 ].pack( "C*" ) File.open( "binary_data", File::CREAT | File::TRUNC | File::WRONLY, 0644 ) do |f| p f.external_encoding # # f.write(binary_data) # test.rb:7:in `write': "\x80" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) # from test.rb:7:in `block in
' # from test.rb:7:in `open' # from test.rb:7:in `
' end How to avoid this issue? You must open the file with BINARY MODE. The file object being binary mode, the +File#write+ method doesn't encode the data automatically. See below: binary_data = [ 0x80, 0x81 ].pack( "C*" ) File.open( "binary_data", "w+b", 0644 ) do |f| p f.external_encoding # # f.write(binary_data) # OK. NO ERROR! end cf. http://ruby-doc.org/ruby-1.9/classes/IO.html -- nobuoka