Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

decode / encode Little-endian UTF-16 Unicode 1

Status
Not open for further replies.

jr8rdt

IS-IT--Management
Feb 9, 2006
59
US
Hi,I have a txt file from winmsd (windows system info) which is in Little-endian UTF-16 Unicode format. I am writing a perl script which suppose to read this file.

Can perl masters in this forum shed some lights to me on how to convert this Little-endian UTF-16 Unicode format to ASCII/UTF-8 without BOM?

I use Encode library but still have problem

use Encode;
while(<DAT>) {
undef $x;
and I have tried both

1) $x= Encode::from_to($_, 'UTF-16LE', 'utf-8');
and
2) $x = encode("UTF-8", $_);

both fails
}
 
How do they fail? Please provide example data and results.
The two methods are different, the first one returns the length of the converted string, the other one the converted string.
Also: if you read a file, the line read into $_ will have a line terminator, so you would first [tt]chomp;[/tt] it. And [tt]undef $x;[/tt] is of no use. Post a short but complete piece of code placed between [ignore]
Code:
...
[/ignore] tags.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
It can be done like this.
[tt]
use strict;
use warnings;
use Encode;

[green]#your givens here
my $infile="d:/test/xyz_utf16LE.txt";
my $outfile="d:/test/xyz_utf8.txt";[/green]
my $octet;

open(DAT,"<",$infile) or die "failed to open $infile: $!";
binmode DAT;
while (<DAT>) {
$octet.=$_;
}
close DAT;

Encode::from_to($octet,"utf16LE","utf8");

$octet=~ s/^\xEF\xBB\xBF//;
open(DAT,">",$outfile) or die "failed to open $outfile: $!";
binmode DAT;
print DAT $octet;
close DAT;
[/tt]
 
tsuji,
your code works. thanks for the help.

YOU ARE DA MAN!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top