views:

32

answers:

1

Hi,

I've a long text file with some invalidad encoded characters in UTF-16. So far, I've been trying to convert it using the following code:

ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-16' )
urf_8_str = ic.iconv( an_invalid_encoded_utf_16_string )

And I get an Iconv::InvalidCharacter exception.

I'm using OS X 10.6 (since it seems that the iconv implementation is a bit special on Mac machines) and Ruby 1.8

Do you know any way to convert a string in Ruby with invalid characters on it?

Thanks !

A: 
ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-16' )
urf_8_str = (ic.iconv(bad_string) rescue 'oops, bad encoding')
Nakilon