gpt4 book ai didi

ruby - 如何避免在解码文件时出现 UndefinedConversionError?

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:57 24 4
gpt4 key购买 nike

我有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com