# Problem with special characters

**URL:** <https://www.ruby-forum.com/t/problem-with-special-characters/160769>\
**Category:** Ruby\
**Created:** [February 19, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160769 "2009-02-19T14:04:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [February 19, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160769/1 "2009-02-19T14:04:47Z")

</div>

Hi:

O.S Windows XP sp3  
Ruby 1.8.6

I am working with ruby using DBI::Mysql, and the returned descriptions  
are not displayed ok. The puts command is displaying wrong characters,  
when the text has special chars like Ã¡.

My program here :

puts “OlÃ¡ Mundo!”; # for test

require “dbi”;

begin  
# connect to the MySQL server  
dbh = DBI.connect(“DBI:Mysql:test:localhost”, “test”,  
“q3der3fe5ahy”);

```
sth = dbh.execute("SELECT * FROM uf ");

sth.each do |row|
    puts 'ID:' + row[0].to_s + ' Mi: ' + row[1] + 'DecriÃ§Ã£o: ' +

```

row[2];  
end  
sth.finish;  
rescue DBI::DatabaseError =\> e  
puts “An error occurred”;  
puts “Error code: #{e.err}”;  
puts “Error message: #{e.errstr}”;  
ensure  
# disconnect from server  
dbh.disconnect if dbh;  
end

//=========== Result  
OlÃŸ Mundo!  
ID:1 Mi: AC DecriÃ¾Ã’o: Acre  
ID:2 Mi: AL DecriÃ¾Ã’o: Alagoas  
ID:3 Mi: AM DecriÃ¾Ã’o: Amazonas  
ID:4 Mi: AP DecriÃ¾Ã’o: AmapÃŸ  
ID:5 Mi: BA DecriÃ¾Ã’o: Bahia  
ID:6 Mi: CE DecriÃ¾Ã’o: CearÃŸ  
ID:7 Mi: DF DecriÃ¾Ã’o: Distrito Federal  
ID:8 Mi: ES DecriÃ¾Ã’o: EspÃrito Santo  
ID:9 Mi: GO DecriÃ¾Ã’o: GoiÃŸs  
ID:10 Mi: MA DecriÃ¾Ã’o: MaranhÃ’o  
ID:11 Mi: MG DecriÃ¾Ã’o: Minas Gerais  
ID:12 Mi: MS DecriÃ¾Ã’o: Mato Grosso do Sul  
ID:13 Mi: MT DecriÃ¾Ã’o: Mato Grosso  
ID:14 Mi: PA DecriÃ¾Ã’o: ParÃŸ  
ID:15 Mi: PB DecriÃ¾Ã’o: ParaÃba  
ID:16 Mi: PE DecriÃ¾Ã’o: Pernambuco  
ID:17 Mi: PI DecriÃ¾Ã’o: PiauÃ  
ID:18 Mi: PR DecriÃ¾Ã’o: ParanÃŸ  
ID:19 Mi: RJ DecriÃ¾Ã’o: Rio de Janeiro  
ID:20 Mi: RN DecriÃ¾Ã’o: Rio Grande do Norte  
ID:21 Mi: RO DecriÃ¾Ã’o: RondÂ¶nia  
ID:22 Mi: RR DecriÃ¾Ã’o: Roraima  
ID:23 Mi: RS DecriÃ¾Ã’o: Rio Grande do Sul  
ID:24 Mi: SC DecriÃ¾Ã’o: Santa Catarina  
ID:25 Mi: SE DecriÃ¾Ã’o: Sergipe  
ID:26 Mi: SP DecriÃ¾Ã’o: SÃ’o Paulo  
ID:27 Mi: TO DecriÃ¾Ã’o: Tocantins  
ID:28 Mi: XX DecriÃ¾Ã’o: Fora Do Brasil

How to display the text with special characters like ‘Ã§ Ãº Ã­’ tha are  
used commonly in latin languages?

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [February 19, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160769/2 "2009-02-19T14:58:00Z")

</div>

> Decrição  
> DecriþÒo

Your input is in latin-1[1] but the windows console probably uses cp850  
[2]. You could change the codepage with “mode con cp select=1252” (I  
guess), use other software, or recode the output.

[1] [ISO/IEC 8859-1 - Wikipedia](http://en.wikipedia.org/wiki/ISO-8859-1)  
[2] [Code page 850 - Wikipedia](http://en.wikipedia.org/wiki/Cp850)

---

<div class="post-metadata">

**Author:** ![amichelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@amichelin](https://www.ruby-forum.com/u/amichelin)\
**Post date:** [February 21, 2009, 8:08am UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160769/3 "2009-02-21T08:08:30Z")

</div>

> Ruby 1.8.6
> 
> I am working with ruby using DBI::Mysql, and the returned descriptions  
> are not displayed ok. The puts command is displaying wrong characters,  
> when the text has special chars like á.

[snip]

> ## How to display the text with special characters like ‘ç ú í’ tha are used commonly in latin languages?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I am facing a similar issue on Mac OS X where the TextMate console can  
only  
use UTF8 encoding (The Mac terminal can be set to any encoding).

I asked a similar question to the TextMate mailing list, and I got a  
useable  
workaround. Here is a very small program reading and printing the  
content of  
a text file encoded in ISO-8859-1:

require “iconv”

file = File.open(“test.txt”, “r”)  
file.each {  
|line|  
print line  
print Iconv.iconv(“UTF-8”,“LATIN1”,line)  
}  
file.close

So I guess you can use the iconv library to convert in whichever  
direction  
you may need.

Regards,

JD
