Ruby on Rails - Encoding::UndefinedConversionError: \xC3 from ASCII-8BIT to UTF-8
For anybody that are coming here from google this is how I fixed my encoding error.
right before you declare of open your file add this code
.force_encoding("UTF-8")
so before you declare the file to a variable like this :
csv_file = params[:file].read.force_encoding("UTF-8")
Hope this will help some since I had this problem for a long time, but now it works yay!
ruby `encode': \xC3 from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
It seems you should use another encoding for the object. You should set the proper codepage to the variable @tree
, for instance, using iso-8859-1 instead of ascii-8bit by using @tree.force_encoding('ISO-8859-1')
. Because ASCII-8BIT
is used just for binary files.
To find the current external encoding for ruby, issue:
Encoding.default_external
If sudo solves the problem, the problem was in default codepage (encoding), so to resolve it you have to set the proper default codepage (encoding), by either:
In ruby to change encoding to utf-8 or another proper one, do as follows:
Encoding.default_external = Encoding::UTF_8
In bash,
grep
current valid set up:$ sudo env|grep UTF-8
LC_ALL=ru_RU.UTF-8
LANG=ru_RU.UTF-8Then set them in
.bashrc
properly, in a similar way, but not exactly withru_RU
language, such as the following:export LC_ALL=ru_RU.UTF-8
export LANG=ru_RU.UTF-8
`write': \xCF from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) while writing to file from url
Use force_encoding
:
open(uri) {|url_file| tempfile.write(url_file.read.force_encoding("UTF-8"))
Encoding::UndefinedConversionError: \x96 from ASCII-8BIT to UTF-8 error while writing to a file in ruby
Well, I will try to suggest.
It sounds like you have received this file from a workstation, running Windows. It looks like this file’s original name is
Volunteer Log – in Page.docx
That said, is was stored using Encoding::CP1252
. OK, you are to handle CP1252
in a proper way:
file = File.open 'names', 'w'
file.puts filename.force_encoding(Encoding::CP1252).encode(Encoding::UTF_8)
Hope it helps.
Why do I get a string encoding issue \xE2 from ASCII-8BIT to UTF-8?
The error message is actually being thrown on the file write, not by your encode/decode inside the params, because Ruby is trying to apply default character encoding on file.write
. To prevent this, the quickest fix is to add the b
flag when you open the file
file = File.new base_path + filename, 'wb+'
file.write Base64.decode64( attachment.source['Content'] )
That's assuming the incoming attachment is encoded in Base64, as your code implies (I have no way to verify this). The Base64 encoding stored inside attachment.source['Content']
should be the same bytes in ASCII-8BIT and UTF-8, so there is no point converting it inside the call to decode64
.
Encoding::UndefinedConversionError \xC2 from ASCII-8BIT to UTF-8 with redcarpet
in the end I solved this with adding force_encoding("UFT-8")
to the html
like this:
f.write html.force_encoding("UTF-8")
it fixed it.
Related Topics
Rails with Ruby-Debugger Throw 'Symbol Not Found: _Ruby_Current_Thread (Loaderror)'
How Can an Activerecord::Relation Object Call Class Methods
Ruby: How to Add "# Encoding: Utf-8" Automatically
Sum the Value of Array in Hash
How to Run a Single Rspec Test
Parsing Xls and Xlsx (Ms Excel) Files with Ruby
Ruby on Rails: Alias_Method_Chain, What Exactly Does It Do
Default Task for Namespace in Rake
Getting Rid of Ruby Gems That Won't Die
Getting an Ascii Character Code in Ruby Using '' (Question Mark) Fails
Couldn't Require Openssl in Ruby
Can Someone Explain the Class.Superclass.Class.Superclass Paradox