UCS-2

F

Florian Weber

Hi!

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

Thanks!

Ciao!
Florian
 
D

Dido Sevilla

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.
 
D

dave.burt

Florian said:
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
 
P

Paul Battley

Florian said:
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.
 
T

Tim Bray

Paul said:
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
 

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
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top