Search code examples
perlunicodeansi

Perl - Convert PC UTF-8 to PC ANSI


I have a file that is encoded PC UTF-8. I would like to convert the file into PC ANSI.

I have tried the below, but I always get the output file to be PC UTF-8.

use Encode;

$infile = $ARGV[0];
open(INFILE, $infile);

my $outfile = "temp.txt";

open(OUTFILE, ">$outfile");

while(<INFILE>) {
  my $row = $_;
  chomp $row;

  $row = Encode::encode("Windows-1252", $row);
  print OUTFILE $row."\n";

}

close INFILE;
close OUTFILE;

Solution

  • The problem is that you never decode the data you encode.

    use strict;
    use warnings;
    use Encode qw( encode decode );
    
    open(my $INFILE,  '<', $ARGV[0]) or die $!;
    open(my $OUTFILE, '>', $ARGV[1]) or die $!;
    
    while (my $utf8 = <$INFILE>) {
       my $code_points = decode('UTF-8', $utf8);    # <-- This was missing.
       my $cp1252 = encode('cp1252', $code_points);
       print $OUTFILE $cp1252;
    }
    

    But you can do this a bit more easily:

    use strict;
    use warnings;
    
    open(my $INFILE,  '<:encoding(UTF-8)',  $ARGV[0]) or die $!;
    open(my $OUTFILE, '>:encoding(cp1252)', $ARGV[1]) or die $!;
    
    while (<$INFILE>) {
       print $OUTFILE $_;
    }