我有 somefile
我想使用 base64
进行编码
File.open('data/somefile.edf').read.encoding
=> #<Encoding:UTF-8>
base64_string = Base64.encode64(open("data/somefile.edf").to_a.join)
然后我想解码那个文件
file = open('new_edf.edf', 'w') do |file|
file << Base64.decode64(base64_string)
end
但是我得到一个错误:
Encoding::UndefinedConversionError: "\xE1" from ASCII-8BIT to UTF-8
from (pry):22:in `write'
最佳答案
我认为问题在于默认情况下文件以文本模式打开以进行写入。要解决此问题,请使用二进制模式打开文件:
File.open('new_edf.edf', 'wb') { ... }
您还可以查看其他问题:Ruby 1.9 Base64 encoding write to file error
关于ruby - 如何避免在解码文件时出现 UndefinedConversionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386662/