Ucs-2

Hi!

Is there any way to decode a UCS-2 string with ruby?

Thanks!

Ciao!
Florian

On 8/1/06, Florian W. [email protected] wrote:

Hi!

Is there any way to decode a UCS-2 string with ruby?

Decode to what? You’re probably looking for iconv. There’s a Ruby
binding for that in the standard library, and it should be able to
convert your UCS-2 to almost anything. If you just want to convert it
to UTF-8, I believe the pack/unpack methods for strings and arrays
ought to be able to do the trick.

Florian W. wrote:

Hi!

Is there any way to decode a UCS-2 string with ruby?

irb> require ‘iconv’
=> true
irb> Iconv.iconv(“UTF-8”, “UCS-2”, “\0a\0b”)
=> [“ab”]

Cheers,
Dave

On 01/08/06, [email protected] [email protected] wrote:

Florian W. wrote:

Hi!

Is there any way to decode a UCS-2 string with ruby?

irb> require ‘iconv’
=> true
irb> Iconv.iconv(“UTF-8”, “UCS-2”, “\0a\0b”)
=> [“ab”]

It’s probably worth specifying endianness for portability:

Iconv.iconv(“UTF-8”, “UCS-2BE”, “\0a\0b”)

However, are you sure you want UCS-2, and not its superset UTF-16? In
my experience, specifying UCS-2 instead of UTF-16 is almost always a
bug.

Paul.

Paul B. wrote:

However, are you sure you want UCS-2, and not its superset UTF-16? In
my experience, specifying UCS-2 instead of UTF-16 is almost always a
bug.
In fact, if you want to be pedantic (and when it comes to character
encoding, pedantic is good) UCS-2 doesn’t actually exist any more, last
time I checked. Paul is right, you really mean UTF-16. -Tim