Problem with special characters


#1

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?


#2

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] http://en.wikipedia.org/wiki/ISO-8859-1
[2] http://en.wikipedia.org/wiki/Cp850


#3

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/.

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