Re: Decode/encode Unicode



On Thu, Aug 28, 2008 at 9:51 AM, Kless <jonas.esp@xxxxxxxxxxxxxx> wrote:
How to decode a String type to Unicode?
And, to encode Unicode to String?



This is a trickier question than you probably realize.
You might need to know what encoding you want to transform your string
to and from. Sometimes you may not know - mp3 ID3 tag info is
supposed to be UTF8 for example but often isn't. If you don't specify
an encoding it may or may not end up being encoded based on your
locale settings.

Things I have seen mentioned:

iconv - a unix based library for translating between different
encodings. Requires you tell it what you decoding from and encoding
to.
http://wiki.rubyonrails.org/rails/pages/iconv

unidecode - I have used this translate Unicode strings to ASCII by
simple character mapping- it works about 99% of the time so you will
need error handling for when it goes bonk.

Setting KCODE:

$KCODE = 'UTF8'
or
$KCODE = 'u'
or
use -Ku at the command line. This tells Ruby that you want to be
using UTF8 encoding

require 'jcode'

will give you access to some code developed to deal with Japanese
Unicode encoding that address some of the problems with the String
class struggles with Unicode chars like jlength. Always keep in mind
that a lot of the string methods in Ruby do not work properly with
Unicode because they count letters wrong.

Good luck.

Stephen Boisvert
http://blog.ennuyer.net

.



Relevant Pages

  • Re: eval and unicode
    ... encoding your terminal/file/whatnot is written in. ... you have a byte string that starts with u, then ", then something ... The first item in the sequence is \u5fb9 -- a unicode code point. ...
    (comp.lang.python)
  • Ruby, Unicode - ever?
    ... Why can't ruby use at least ICU libs? ... proper Unicode support, don't try to cheat me, that it's OK and enough, ... Ruby String class in current state is TOO MUCH OVERLOADED: ... encoding is senseless - this is plain bit stream. ...
    (comp.lang.ruby)
  • Re: Why asci-only symbols?
    ... >> Perhaps string equivalence in keys will be treated like numeric equivalence? ... I know typewill be and in itself contain no encoding information now, ... >and a Unicode string, the system default encoding ...
    (comp.lang.python)
  • Re: Attention: European C/C++/C#/Java Programmers-Call for Input
    ... and strings in Unicode - many modern languages allow it. ... proprietary half-baked encoding that is incompatible with every other tool ... tools for this new language whose codes will never be seen by its users. ... the effective string length is 1.0x or rare ...
    (comp.arch.embedded)
  • Re: Unicode drives me crazy...
    ... every string on some level). ... Python needs to know what encoding is used. ... The decode instruction converts s into a unicode string - where Python ...
    (comp.lang.python)