Converting file from utf-16 to utf-8

K

Kioko --

Hi,

I'm currently rewriting an old php-script in ruby.
One thing this script is for is to open a file which is in utf-16, read
it as utf-8, work a little bit with the data and save it into a
database.

However I tried to use Iconv to solve this but I've failed ;)

The php script runs perfectly without any error.
If somebody is familiar with php here is the code (yeah, very VERY ugly,
but that's why I'm rewriting it :))

$fp = fopen($file,"r");
$lines = file($file);
for ($line=1; $line<=count($lines); $line++) {
$string .= fgets($fp);
}
$string = iconv('UTF-16', 'UTF-8', $string);


But Ruby throws an Iconv::IllegalSequence error:

doc = File.open(file).read
doc = Iconv.iconv('utf-16', 'utf-8', doc) #Iconv::IllegalSequence

Because it's the same file I really don't know how to solve this.
If I'm using utf-16//IGNORE, obviously, many signs are missing.
Are any other "workarounds"/methods to use out there?
Or something I overlooked?

sincerely.
 
K

Kioko --

OZAWA said:
Iconv.iconv takes "to" encoding first, followed by "from" encoding.

Oh man.. that's really... uhm.. *head->wall*
Read the documentation many times and didn't notice it.

I'm really sorry to have opened such a thread T__T
But thanks for the answer and not flaming about it ;)
 
W

Walton Hoops

OZAWA Sakuro wrote:


Oh man.. that's really... uhm.. *head->wall*
Read the documentation many times and didn't notice it.

I'm really sorry to have opened such a thread T__T
But thanks for the answer and not flaming about it ;)
Heh, I'm sure everyone here has never, *ever*, _*ever*_ made a mistake
like that.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top