Hi,

I know the reason of this bug now. Under Windows, you have an implicit 
":crlf"-layer in all "file-in-out-actions" which do this s/\n/\r\n/gs (or vice 
versa) things. For UTF-16, the :crlf layer is on the wrong side of the pipe ( 
internal -> UTF-16 -> crlf-conversion) because UTF-16 is treated as any other 
text-format. Correct is internal -> crlf-conversion -> UTF-16. So, the correct 
workaround for all multibyte-unicode-formats (except UTF-8, because it encodes 
\n as "unibyte") should be:

open FH, ">:raw", "test8.txt" or die "create $outFile: $!";
binmode(FH, ":encoding(ucs2le):crlf");
print FH "test\ntest\ntest";
close FH;

I know, there are MTOWTDI ;-)

Found here:
http://groups.google.de/group/perl.perl5.porters/browse_thread/thread/bf9b4b0d7175882a/807b7381436ba8e6



Greetings

Kim


> -----Ursprüngliche Nachricht-----
> Von: $Bill Luebkert [mailto:[EMAIL PROTECTED] 
> Gesendet: Dienstag, 2. August 2005 18:54
> An: Neunert Kim
> Betreff: Re: Encoding \n in utf-16
> 
> 
> Neunert Kim wrote:
> 
> > Hi,
> > 
> > Now I have a problem encoding ucs-2/UTF-16 (either LE or 
> BE). See this 
> > small snippet:
> > 
> > open FH, ">:encoding(UTF-16LE)", "test8.txt" or die "create 
> $outFile: 
> > $!"; print FH "test\ntest\ntest"; close FH;
> > 
> > Have a look a the hexdump:
> > 0000000   t  \0   e  \0   s  \0   t  \0  \r  \n  \0   t  \0 
>   e  \0   s
> > 0000020  \0   t  \0  \r  \n  \0   t  \0   e  \0   s  \0   t  \0
> > 0000036
> > 
> > The way \n is encoded seems to be wrong. I think: "\r \n 
> \0" is wrong. 
> > It should be: "\r \0 \n \0", isn´t it. Can anyone help me?
> 
> You could do the encoding and then write in binmode format:
> 
> use strict;
> use warnings;
> use Encode qw(encode);
> 
> my $File = "txt/utf16.txt";
> my $str = "Now is the time for all good men to come to the 
> aid of their country.\ntesting 1234567890\n"; $str =~ s/\n/\r\n/gs;
> 
> my $bytes = encode ('UTF-16', $str);
> 
> open OUT, ">$File" or die "create $File: $!";
> binmode OUT;
> print OUT $bytes;
> close OUT;
> 
> __END__
> 
> 
> 
> -- 
>   ,-/-  __      _  _         $Bill Luebkert    
> Mailto:[EMAIL PROTECTED]
>  (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
>   / ) /--<  o // //      Castle of Medieval Myth & Magic 
> http://www.todbe.com/
> -/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My 
> Perl/Lakers stuff)
> 

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to