Invalid Multibyte Char (Utf-8) Error, Ruby

invalid multibyte char (UTF-8) Error, Ruby

What format is this file in? Are you sure it's UTF-8 and not Windows 1252 as is the default in Windows?

In Ruby 1.9, the header in your file needs to indicate the actual formatting used:

# encoding: UTF-8

If that doesn't work, you may need to experiment with others:

# encoding: Windows-1252

Another common format is ISO Latin1:

# encoding: ISO-8859-1

Both 1252 and 8859-1 are single-byte character sets, each character is always one byte, where UTF-8 is variable length, each character is one or more bytes.

If you need to convert between formats, usually you can open in an editor that's encoding aware and "Save As..." with the encoding you want. Otherwise you might try using iconv to convert it for you.

Ruby invalid multibyte char error (Sep 2019)

I could reproduce your issue by saving the file with ISO-8859-1 encoding.

Running your code with the file in this non UTF8-encoding the error popped up. My solution was to save the file as UTF-8.

I am using Sublime as text editor and there is the option 'file > save with encoding'. I have chosen 'UTF-8' and was able to run the script.

Using puts line.encoding showed me UTF-8 then and no error anymore.

I suggest to re-check the encoding of your saved script file again.

Invalid multibyte char with Ruby 1.9, setting encoding: utf-8 does not fix it

Use a tool such as od (assuming you're on Linux) to look at the actual contents of the file. If you have single byte C5, your file isn't in UTF-8. It's probably stored in ISO-8859-1 or similar, and Ruby is quite correctly complaining.

VSCode complains a Ruby UTF-8 file has invalid multibyte char (US-ASCII)

I think the issue is in the linter.

"ruby.lint": {
"reek": true,
"rubocop": true,
"ruby": {
"unicode": true,
},
"fasterer": true,
"debride": false,
"ruby-lint": false
},

in settings.json unicode is not turned on by default for ruby.lint so you need to do that manually.

Rails 3.1 invalid multibyte char (UTF-8)

If you remove the escape slash from the Euro sign this regexp will compile properly and work.

Ruby 1.9 invalid multibyte char (UTF-8)

Try:

some_string.gsub(/\u2026/)

You can also take a look at this question for more information.

Ruby error invalid multibyte char (US-ASCII)

When you run the script with Ruby 1.9, change the first two lines of the script to:

#!/usr/bin/env ruby
# encoding: utf-8
require 'net/http'

This tells Ruby to run the script with support for the UTF-8 character set. Without that line Ruby 1.9 would default to the US_ASCII character set.

Just for the record: This will not work in Ruby 1.8, because 1.8 doesn't knew anything about string encodings. And the line is not needed anymore in Ruby 2.0, because Ruby 2.0 is using UTF-8 as the default anyway.



Related Topics



Leave a reply



Submit