# Ruby1.9: Encoding problems (how to use #force\_encoding ?)

**URL:** <https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772>\
**Category:** Ruby\
**Created:** [September 2, 2009, 12:41am UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772 "2009-09-02T00:41:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 12:41am UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/1 "2009-09-02T00:41:24Z")

</div>

Hi, I’m using geo\_location Ruby gem which returns to me a hash with the  
given  
IP geolocation.

I use Ruby1.9 and UTF-8 works fine, but in this case, when the “city”  
has  
“strange” symbols the the gem gives the string encoded in ASCII-8BIT.

For example:

```
Alarc'n (theorically it should be "AlarcÃ³n")

```

I need to send this string to a server which mandates UTF-8 usage so  
sending  
it as it’s fails.

I’ve tryed to convert the encoding but received an error:

result.encode “UTF-8”  
=\> `encode’: “\xF3” from ASCII-8BIT to UTF-8  
(Encoding::UndefinedConversionError)

I’ve also tryed with force\_encoding:  
result.force\_encoding “UTF-8”

and then, the “result” string is converted to UTF-8 (I’ve checked  
result.encoding) but it’s also not valid for the server and when  
printing it I  
see the same as before.

I need all of this just for a simple demo, so it owuld be valid for me  
just to  
delete the non valid UTF-8 chars from the result string, but I don’t  
know  
how to do it.

Any help please?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/2 "2009-09-02T14:15:00Z")

</div>

IÃ±aki Baz C. wrote:

> Hi, I’m using geo\_location Ruby gem which returns to me a hash with the  
> given  
> IP geolocation.

Lots of gems are not ruby-1.9 compatible. You should probably report  
problems to the author, ideally with a patch which fixes it, and a test  
case which reproduces it.

> I use Ruby1.9 and UTF-8 works fine, but in this case, when the “city”  
> has  
> “strange” symbols the the gem gives the string encoded in ASCII-8BIT.

All data read from a socket is tagged as ASCII-8BIT by default. That’s  
probably what’s happening in the library you’re using.

> I need to send this string to a server which mandates UTF-8 usage so  
> sending  
> it as it’s fails.

That doesn’t make much sense. A string, when it hits a socket, is just a  
stream of bytes. So you should be sending the same stream of bytes as  
you receive.

> I’ve tryed to convert the encoding but received an error:
> 
> result.encode “UTF-8”  
> =\> `encode’: “\xF3” from ASCII-8BIT to UTF-8  
> (Encoding::UndefinedConversionError)

That’s correct. Transcoding tries to _transcode_ (replace characters one  
at a time), and these high characters in ASCII-8BIT have no Unicode  
equivalents.

> I’ve also tryed with force\_encoding:  
> result.force\_encoding “UTF-8”
> 
> and then, the “result” string is converted to UTF-8 (I’ve checked  
> result.encoding)

It’s not converted, it’s just tagged as being a string of UTF-8  
characters, which it sounds like it is.

> but it’s also not valid for the server

Again, doesn’t mean much without seeing the code which is trying to  
submit this to the server.

> I need all of this just for a simple demo, so it owuld be valid for me  
> just to  
> delete the non valid UTF-8 chars from the result string, but I don’t  
> know  
> how to do it.

str.force\_encoding(“ASCII-8BIT”) # if not already  
str.gsub!(/[^\x20-\x7e]/,‘’)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/3 "2009-09-02T15:01:26Z")

</div>

Dear Iñaki,

maybe you can use CGI.escape for your encoding problems?

I fetched the Spanish wikipedia page for Alarcón like so:

# encoding: utf-8

require “cgi”  
require ‘open-uri’

search\_what=CGI.escape(“Alarcón”)  
page=“[Buscar - Wikipedia, la enciclopedia libre](http://es.wikipedia.org/w/index.php?title=Especial%3ABuscar&search=#%7Bsearch_what%7D&fulltext=Buscar)”  
open(page){ |f| print f.read }

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/4 "2009-09-02T17:25:28Z")

</div>

On Sep 2, 2009, at 9:59 AM, Iñaki Baz C. wrote:

> > require ‘open-uri’
> > 
> > search\_what=CGI.escape(“Alarcón”)  
> > page="[Buscar - Wikipedia, la enciclopedia libre](http://es.wikipedia.org/w/index.php?title=Especial%3ABuscar&search=#%7Bsearch_what%7D&fulltext=Buscar)  
> > "  
> > open(page){ |f| print f.read }
> 
> Thanks but the problem is that the geo\_location Ruby gem returns a  
> wrong string (encoded in ASCII-8BIT) since it contains invalid chars  
> for ASCII-8BIT encoding, so Ruby fails when trying to convert it to  
> other encoding ☹

There are no invalid characters in ASCII-8BIT. It’s a catch all  
Encoding. So that’s definitely not the problem… 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/5 "2009-09-02T21:46:01Z")

</div>

El Miércoles, 2 de Septiembre de 2009, James Edward G. II  
escribió:

> There are no invalid characters in ASCII-8BIT. It’s a catch all  
> Encoding. So that’s definitely not the problem… 😉

Ok, that’s a good point.  
I’ll try it.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 13, 2012, 5:09pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/6 "2012-01-13T17:09:32Z")

</div>

“That’s correct. Transcoding tries to _transcode_ (replace characters  
one  
at a time), and these high characters in ASCII-8BIT have no Unicode  
equivalents.”

It isn’t true, in fact, \xc characters are Unicode code points (in UTF-8  
encoding) and not ASCII-2 characters. You have a Unicode String with  
UTF-8 encoding and encoding incorrectly set to ASCII-2, to solve this  
problem try this:

begin  
str.encode! Encoding::UTF\_8 if str.encoding != Encoding::UTF\_8  
rescue Encoding::UndefinedConversionError  
#string incorrectly encoded try force  
str.force\_encoding Encoding::UTF\_8  
end

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 2, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/7 "2009-09-02T17:00:52Z")

</div>

2009/9/2 Axel E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> search\_what=CGI.escape(“AlarcÃ³n”)  
> page=“[Buscar - Wikipedia, la enciclopedia libre](http://es.wikipedia.org/w/index.php?title=Especial%3ABuscar&search=#%7Bsearch_what%7D&fulltext=Buscar)”  
> open(page){ |f| print f.read }

Thanks but the problem is that the geo\_location Ruby gem returns a  
wrong string (encoded in ASCII-8BIT) since it contains invalid chars  
for ASCII-8BIT encoding, so Ruby fails when trying to convert it to  
other encoding ☹

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 14, 2012, 9:52am UTC](https://www.ruby-forum.com/t/ruby1-9-encoding-problems-how-to-use-force-encoding/174772/8 "2012-01-14T09:52:06Z")

</div>

Pedro G. wrote in post #1040715:

> “That’s correct. Transcoding tries to _transcode_ (replace characters  
> one  
> at a time), and these high characters in ASCII-8BIT have no Unicode  
> equivalents.”
> 
> It isn’t true

Yes, it _is_ true, because it’s exactly what the Ruby encoding  
“ASCII\_8BIT” means. It allows you to use \x80 to \xFF without defining  
what character set those are in. Hence these characters cannot be  
transcoded, since it’s undefined what they are.

(Also, why are you resurrecting a 2-year-old thread?)

> , in fact, \xc characters are Unicode code points (in UTF-8  
> encoding) and not ASCII-2 characters. You have a Unicode String with  
> UTF-8 encoding and encoding incorrectly set to ASCII-2, to solve this  
> problem try this:

What do you mean by ASCII-2? Standard ASCII is only a 7-bit character  
set. There are a whole bunch of 8-bit extensions to ASCII, e.g.  
ISO-8859-1, Windows-1252 etc. They all define different character sets  
for \x80 to \xff. The encoding “ASCII\_8BIT” makes no assertion about  
what these high characters are.

> begin  
> str.encode! Encoding::UTF\_8 if str.encoding != Encoding::UTF\_8  
> rescue Encoding::UndefinedConversionError  
> #string incorrectly encoded try force  
> str.force\_encoding Encoding::UTF\_8  
> end

That’s wrong, and just shows you don’t understand the problem.
