#!perl -w # usage: perl eight2sixteen.pl INFILE OUTFILE use strict; my($in,$out) = @ARGV; open(IN,$in) or die "cannot open $in $!"; open(OUT,">$out") or die "cannot open $out $!"; binmode OUT; use utf8; # treat input as utf8 characters, not byte series. use Unicode::String 'utf8','byteswap2', 'utf16'; # make sure the byte order mark is present. my $firstString; $firstString = ; if (ord($firstString) != 0xfeff) { # not present. Construct it and print it to OUT. my $bom = Unicode::String->new(); $bom->chr(0xfeff); print OUT byteswap2($bom->utf16); } $firstString = utf8($firstString); print OUT byteswap2($firstString->utf16); # print all remaining lines. while () { my($u) = utf8($_); print OUT byteswap2($u->utf16); } close(IN) or die "cannot close $in $!"; close(OUT) or die "cannot close $out $!";