If you want to convert unicode to ascii, you should know that some characters cannot be converted, because they simply do not exist in ascii. If you can live with this, you can try the following:
#!/usr/bin/env perl use strict; use warnings; use autodie; use open IN => ':encoding(UTF-16)'; use open OUT => ':encoding(ascii)'; my $buffer; open(my $ifh, '<', 'utf16bom.txt'); read($ifh, $buffer, -s $ifh); close($ifh); open(my $ofh, '>', 'ascii.txt'); print($ofh $buffer); close($ofh);
If you don't have autodie, just delete this line - you should then change your open / close statements with
open(...) or die "error: $!\n";
If you have characters that cannot be converted, you will receive warnings on the console, and your output file will have, for example. text as
\x{00e4}\x{00f6}\x{00fc}\x{00df}
. BTW: If you donโt have a mom, but know that itโs Big Endian (Little Endian), you can change the encoding line to
use open IN => ':encoding(UTF-16BE)';
or
use open IN => ':encoding(UTF-16LE)';
Hope it works on Windows as well. I canโt try now.
source share