Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by jira0004 (Monk)
on Jun 17, 2005 at 04:11 UTC ( [id://467551]=perlquestion: print w/replies, xml ) Need Help??

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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://467551]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found