# Problem with special characters

**URL:** <https://www.ruby-forum.com/t/problem-with-special-characters/160883>\
**Category:** wxRuby\
**Created:** [February 20, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883 "2009-02-20T16:21:13Z")\
**Posts on this page:** 5\
**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 20, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883/1 "2009-02-20T16:21:13Z")

</div>

Hi:

I am makeing a basic program with wxruby.

Is a frame with a Wx::Notebook inside.

The notebook has two tabs.

The first tab has a grid with 3 columns.

I am using dbi to load data from database, to fill the rows.

But the column 3 that is a description, is not fill when the  
description  
has special characters like Â‘Ã§, Ã©, Ã±Â’;

But dbi returns the special characters, i create a file to write the  
returned data for test.

And when i edit the file the text are ok.

My program is :

require ‘wx’;

require “dbi”;

class BasicGridApp \< Wx::App

```
def on_init

    begin

        @frame = Wx::Frame.new( nil , -1 , "Basic Grid Application",

                                 Wx::Point.new(10,10),

                                 Wx::Size.new(760,550) );

        @note = Wx::Notebook.new( @frame, -1, Wx::Point.new(0,0),

```

Wx::Size.new(400,400), 0,

```
                                    "NoteBook" );

        @TabWindow = Wx::Window.new( @note, -1, Wx::Point.new(0,0),

```

Wx::Size.new(100,100), 0, “Tab1”);

```
        @TabWindow1 = Wx::Window.new( @note, -1, Wx::Point.new(0,0),

```

Wx::Size.new(100,100), 0, “Tab2”);

```
        @grid = Wx::Grid.new( @TabWindow , -1, Wx::Point.new(0,0),

```

Wx::Size.new(400,400), 0, “Grid”);

```
        @grid.create_grid( 40 , 4 );

        @grid.set_col_label_value( 0, "ID" );

        @grid.set_col_label_value( 1, "AvreviaÃ§Ã£o" );

        @grid.set_col_label_value( 2, "DescriÃ§Ã£o" );

        @grid.set_col_label_size( 24 );

        @grid.set_row_label_size( 24 );

        # connect to the MySQL server

        dbh = DBI.connect("DBI:Mysql:test:localhost", "test",

```

“er5aa5h”);

```
        sth = dbh.execute("SELECT iduf, ufdesc, descricao COLLATE

```

latin1\_bin AS descricao FROM uf ");

```
        contador = 0;

        sth.each do |row|

            @grid.set_col_format_number( 0 );

            @grid.set_cell_value( contador, 0, row[0].to_s ); # ID

            @grid.set_cell_value( contador, 1, row[1] ); # 

```

short  
description

```
            @grid.set_cell_value( contador, 2, row[2] ); #

```

description

```
            contador += 1;

        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

    @note.add_page( @TabWindow, "Titulo1", true, -1);

    @note.add_page( @TabWindow1, "Titulo2", false, -1);

    @frame.show();

end

```

end

b = BasicGridApp.new();

b.main\_loop();

Can you give my some ligth?

Thanks in advance

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090219-0, 19/02/2009  
Tested on: 20/02/2009 11:14:33  
avast! - copyright (c) 1988-2009 ALWIL Software.

---

<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 20, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883/2 "2009-02-20T20:20:48Z")

</div>

> I am using dbi to load data from database, to fill the rows.
> 
> But the column 3 that is a description, is not fill when the  
> description has special characters like ‘ç, é, ñ’;

A wxRuby application can display any character of any script - but any  
string that’s passed into wxRuby must be UTF-8.

I would guess that your database is using some 8-bit encoding like  
iso-8859-1. You should either:

1. convert your database to use utf-8 internally. This is the best  
solution if you’re able to design it from the start.

2. convert strings coming from the database to utf-8, using Ruby’s  
standard ‘Iconv’ library, or, if you’re using Ruby 1.9, new string  
methods like “encode!”.

hth  
alex

---

<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 20, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883/3 "2009-02-20T21:49:18Z")

</div>

Alex :

Thanks is working.

I have a question

descricao = Iconv.iconv( ‘utf-8’, ‘cp1252’, row[2] );

@grid.set\_cell\_value( contador, 2, descricao.to\_s );

What i need make to\_s to the result of Iconv.iconv?

If i put descricao wihtout to\_s the set\_cell\_value give error  
Â“set\_cell\_value: can’t convert Array into String (TypeError)Â”

Iconv.iconv returns array type?

Thanks in advance

De: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Em nome de Alex F.  
Enviada em: sexta-feira, 20 de fevereiro de 2009 16:06  
Para: General discussion of wxRuby  
Assunto: Re: [wxruby-users] Problem with special characters

> I am using dbi to load data from database, to fill the rows.
> 
> But the column 3 that is a description, is not fill when the  
> description has special characters like Â‘Ã§, Ã©, Ã±Â’;

A wxRuby application can display any character of any script - but any  
string that’s passed into wxRuby must be UTF-8.

I would guess that your database is using some 8-bit encoding like  
iso-8859-1. You should either:

1. convert your database to use utf-8 internally. This is the best  
solution if you’re able to design it from the start.

2. convert strings coming from the database to utf-8, using Ruby’s  
standard ‘Iconv’ library, or, if you’re using Ruby 1.9, new string  
methods like “encode!”.

hth  
alex

* * *

wxruby-users mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Inbound message clean.

Virus Database (VPS): 090219-0, 19/02/2009  
Tested on: 20/02/2009 16:45:44  
avast! - copyright (c) 1988-2009 ALWIL Software.

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090219-0, 19/02/2009  
Tested on: 20/02/2009 17:27:11  
avast! - copyright (c) 1988-2009 ALWIL Software.

---

<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, 3:33am UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883/4 "2009-02-21T03:33:35Z")

</div>

On Fri, Feb 20, 2009 at 2:27 PM, (gmail) Alejandro Michelin Salomon \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> descricao = Iconv.iconv( ‘utf-8’, ‘cp1252’, row[2] );
> 
> @grid.set\_cell\_value( contador, 2, descricao.to\_s );

The thing about Iconv.iconv() is that it always returns an array of  
strings,  
even if you just pass 1 string, so if your just passing 1 string, then  
you  
need to use @grid.set\_cell\_value( contador, 2, descricao[0]).

hth,

Mario

---

<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, 1:19pm UTC](https://www.ruby-forum.com/t/problem-with-special-characters/160883/5 "2009-02-21T13:19:23Z")

</div>

Mario :

Works fine.

Thanks a lot.

De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Em nome de Mario S.  
Enviada em: sexta-feira, 20 de fevereiro de 2009 23:29  
Para: General discussion of wxRuby  
Assunto: Re: [wxruby-users] RES: Problem with special characters

On Fri, Feb 20, 2009 at 2:27 PM, (gmail) Alejandro Michelin Salomon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Alex :

Thanks is working.

I have a question

descricao = Iconv.iconv( ‘utf-8’, ‘cp1252’, row[2] );

@grid.set\_cell\_value( contador, 2, descricao.to\_s );

The thing about Iconv.iconv() is that it always returns an array of  
strings,  
even if you just pass 1 string, so if your just passing 1 string, then  
you  
need to use @grid.set\_cell\_value( contador, 2, descricao[0]).

hth,

Mario

–  
Mario S.  
[http://www.trilake.net](http://www.trilake.net)  
[http://www.ruby-im.net](http://www.ruby-im.net)  
[http://rubyforge.org/projects/wxruby/](http://rubyforge.org/projects/wxruby/)  
[http://rubyforge.org/projects/wxride/](http://rubyforge.org/projects/wxride/)

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Inbound message clean.

Virus Database (VPS): 090220-0, 20/02/2009  
Tested on: 21/02/2009 09:02:31  
avast! - copyright (c) 1988-2009 ALWIL Software.

* * *

avast! Antivirus [http://www.avast.com](http://www.avast.com) : Outbound message clean.

Virus Database (VPS): 090220-0, 20/02/2009  
Tested on: 21/02/2009 09:15:26  
avast! - copyright (c) 1988-2009 ALWIL Software.
