Encoding::UndefinedConversionError: \xC2 from ASCII-8BIT to UTF-8
Your string is in some other encoding, most likely iso-8859-1, so you should run this to convert it:
"\xC2".encode("iso-8859-1").force_encoding("utf-8")
=> "Ã"
See this question for more information related to this issue.
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.
`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"))
\xC2 to UTF-8 in conversion from ASCII-8BIT to UTF-8
It turns out the page had an invalid character (an interpunct '·'), which I found out with the following code (credits to this gist and this question):
lines = IO.readlines("app/views/layouts/application.html.haml").map do |line|
line.force_encoding('ASCII-8BIT').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?')
end
File.open("app/views/layouts/application.html.haml", "w") do |file|
file.puts(lines)
end
After running this code, I could find the problematic characters with a simple git diff
and moved the code to a helper file with # encoding: utf-8
at the top.
I'm not sure why this doesn't fail with MRI but it should since I'm not specifying the encoding of the haml file.
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.
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
Encoding::UndefinedConversionError (\xE2 from ASCII-8BIT to UTF-8): with Rails 4.1 & mysql BLOB datatype
FYI .. it is resolved i changed Data type from BLOB to TEXT and it worked.
BLOB values are treated as binary strings (byte strings). They have no
character set, and sorting and comparison are based on the numeric
values of the bytes in column values. TEXT values are treated as
nonbinary strings (character strings). They have a character set, and
values are sorted and compared based on the collation of the character
set.
The BLOB and TEXT Types
Related Topics
Access 'Self' of an Object Through the Parameters
My Class Name Conflicting with Ruby'S
Devise Rendering Default Views from Gem Instead of Generated Ones
How to Display a Regexp Output in an Alphabetical List
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
Rails 3 - How to Handle Pg Error Incomplete Multibyte Character
Convert Spreadsheet Column Index into Character Sequence
Rails 4 Not Encrypting Cookie Contents
Ruby CSV.Each in While Loop Not Executing Second Time Through
Why Does Ruby Use Its Own Syntax for Safe Navigation Operator
What Is an Eoferror in Ruby File I/O
Rails: Uninitialized Constant Just Happen on Production Server
Generate Full Url to JavaScript in Rails (Similar to JavaScript_Path, But Url)
Determining Method's Visibility on the Fly