jira0004 has asked for the wisdom of the Perl Monks concerning the following question:

I have some text that is UTF-8 but I need to convert it correctly to UTF-16LE. What do I do? How do I convert text from UTF-8 to UTF-16LE.

Thanks,

Peter Jirak

  • Comment on How do I convert text from UTF-8 to UTF-16LE in Perl

Replies are listed 'Best First'.
Re: How do I convert text from UTF-8 to UTF-16LE in Perl
by JamesNC (Chaplain) on Jun 17, 2005 at 04:49 UTC
    I think Encode does what you want...
    use Encode; my $data = "some utf8 string"; Encode::from_to( $data, "utf8", "utf16le" ); print $data;

    JamesNC
Re: How do I convert text from UTF-8 to UTF-16LE in Perl
by GrandFather (Saint) on Jun 17, 2005 at 05:22 UTC

    From Perl Cookbook ISBN 0596003137:

    open(my $ifh, "<:encoding(ENCODING_NAME)", $filename); open(my $ofh, ">:encoding(ENCODING_NAME)", $filename);

    Perl is Huffman encoded by design.
      Actually, since the input is utf8, the "more natural" way (with the "blanks" filled in) would be:
      open( my $ifh, "<:utf8", $iname) or die "$iname: $!"; open( my $ofh, ">:encoding(UTF-16LE)", $oname ) or die "$oname: $!"; print $ofh while (<$ifh>);
Re: How do I convert text from UTF-8 to UTF-16LE in Perl
by bravenmd (Sexton) on Jun 17, 2005 at 17:55 UTC
    Try taking a look at the Unicode-Transform-0.32 module posted on CPAN. The basic idea is as follows: <SRC_UTF_NAME>_to_<DST_UTF_NAME>(CALLBACK, STRING). The code posted by JamesNC should also work.