Ruby on rails RoR ASCII-8bit到UTF-8,在Net::HTTP.get_response.body中使用非拉丁(西里尔)符号
我需要通过Net::HTTP获取一些数据,通过接收ASCII-8bit的响应,它工作良好。问题是如何将其编码为utf8并保存所有非拉丁符号 使用Ruby on rails RoR ASCII-8bit到UTF-8,在Net::HTTP.get_response.body中使用非拉丁(西里尔)符号,ruby-on-rails,8-bit,Ruby On Rails,8 Bit,我需要通过Net::HTTP获取一些数据,通过接收ASCII-8bit的响应,它工作良好。问题是如何将其编码为utf8并保存所有非拉丁符号 使用@content.encode('utf-8','binary',:invalid=>:replace, :undef=>:replace,:replace=>“”)我丢失了所有西里尔字母符号 使用@content.encode('utf-8','binary')我从ASCII-8BIT到utf-8得到“\xCB”错误 使用@content.force_
@content.encode('utf-8','binary',:invalid=>:replace,
:undef=>:replace,:replace=>“”)
我丢失了所有西里尔字母符号
使用@content.encode('utf-8','binary')
我从ASCII-8BIT到utf-8得到“\xCB”
错误
使用@content.force_编码(“UTF-8)
我得到������ 而不是西里尔符号
我无法通过谷歌搜索找到答案。问题通过
begin
cleaned = response.body.dup.force_encoding('UTF-8')
unless cleaned.valid_encoding?
cleaned = response.body.encode( 'UTF-8', 'Windows-1251' )
end
content = cleaned
rescue EncodingError
content.encode!( 'UTF-8', invalid: :replace, undef: :replace )
end
问题通过
begin
cleaned = response.body.dup.force_encoding('UTF-8')
unless cleaned.valid_encoding?
cleaned = response.body.encode( 'UTF-8', 'Windows-1251' )
end
content = cleaned
rescue EncodingError
content.encode!( 'UTF-8', invalid: :replace, undef: :replace )
end