How to convert a string's charset??

N

Nanyang Zhan

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

Paul Duncan

--LSp5EJdfMPwZcMS1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Nanyang Zhan ([email protected]) said:
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=20
# (be careful: the parameter order is TO, FROM)
ic =3D Iconv.new('UTF-8', 'BIG5')

# convert string
dst_str =3D ic.iconv(src_str)

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--LSp5EJdfMPwZcMS1
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGF36GzdlT34LClWIRArzBAJ9ass7xYZDYA8uPLZ+LF5eXyyPVfgCfZvT0
Bt1+4c/yu/kQyNzPPNBDA1A=
=T1+Z
-----END PGP SIGNATURE-----

--LSp5EJdfMPwZcMS1--
 
P

Paul Duncan

--vKFfOv5t3oGVpiF+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Nanyang Zhan ([email protected]) said:
Paul Duncan wrote:
=20
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.

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--vKFfOv5t3oGVpiF+
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGGEAnzdlT34LClWIRAnSJAJ98wDUWvcjpBMzy2WBNRtZfkpoXhACgm7uc
9pDr+aFQiiXzr7IM2v4euRs=
=aewt
-----END PGP SIGNATURE-----

--vKFfOv5t3oGVpiF+--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top