Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - Convert PC UTF-8 to PC ANSI

Tags:

unicode

perl

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;
like image 214
user333746 Avatar asked Dec 21 '22 09:12

user333746


1 Answers

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 $_;
}
like image 110
ikegami Avatar answered Jan 05 '23 03:01

ikegami