[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to convert a string's charset??

Nanyang Zhan

4/7/2007 9:22:00 AM

I want to convert a string's charset from GB2312 or BIG5 to UTF-8. How
can i do
this?

--
Posted via http://www.ruby-....

3 Answers

Paul Duncan

4/7/2007 11:21:00 AM

0

* Nanyang Zhan (sxain@hotmail.com) wrote:
> I want to convert a string's charset from GB2312 or BIG5 to UTF-8. How
> can i do
> this?

Use Iconv:

# load library
require 'iconv'

# create iconv object
# (be careful: the parameter order is TO, FROM)
ic = Iconv.new('UTF-8', 'BIG5')

# convert string
dst_str = ic.iconv(src_str)

--
Paul Duncan <pabs@pablotron.org> pabs in #ruby-lang (OPN IRC)
http://www.pabl... OpenPGP Key ID: 0x82C29562

Nanyang Zhan

4/7/2007 6:44:00 PM

0

Paul Duncan wrote:

> Use Iconv:
Thanks.
It works,but some strings will raise error, I have to skip them.

--
Posted via http://www.ruby-....

Paul Duncan

4/8/2007 1:07:00 AM

0

* Nanyang Zhan (sxain@hotmail.com) wrote:
> Paul Duncan wrote:
>
> > Use Iconv:
> Thanks.
> It works,but some strings will raise error, I have to skip them.

You can tell iconv to transliterate or ignore characters that it can't
convert. Appendd //TRANSLIT or //IGNORE to the name of the destination
character set.

--
Paul Duncan <pabs@pablotron.org> pabs in #ruby-lang (OPN IRC)
http://www.pabl... OpenPGP Key ID: 0x82C29562