chell
chell

Reputation: 7866

How to change html encoded character to ascii character

I have a french character that is encoded as follows:

"Jos\xE9e"

I need to convert it to regular character because it produces this error on my server:

invalid byte sequence in UTF-8

What can I do to fix this error?

Rails 3 Ruby 1.9.2

Upvotes: 1

Views: 1062

Answers (3)

Googol
Googol

Reputation: 184

Use a editor support utf-8, and add coding line at the top of all source files:

# coding: utf-8

If some input string is not utf-8, convert it to utf-8 first before processing:

input_str = "Jos\xE9e"
utf_input = input_str.force_encoding('iso-8859-1').encode('utf-8')

All above only work under ruby 1.9. For more information, you can check the book: Ruby Best Practices.

Upvotes: 3

lidaobing
lidaobing

Reputation: 1075

you should use utf8 in all your source code, how about save your file in utf-8 encoding

Upvotes: 0

mu is too short
mu is too short

Reputation: 434616

That looks like "Josée" encoded in ISO 8859-1 (AKA Latin-1). You can use Iconv to convert it to UTF-8:

require 'iconv'
utf_string = Iconv.conv('UTF-8', 'ISO-8859-1', "Jos\xE9e")

Upvotes: 4

Related Questions