> Ed Chester wrote: > >>>Hi all. >>>I have a problemette with the following scripting issue. >>>Its not primarily a technical problem... I quickly wrote a >>>script that >>>did exactly what I want, if possibly inelegantly. The problem >>>I have is that a colleague wrote, (get this) a _visual basic_ >>><shudder>, program that does the same thing about 100 times >>>faster. I am devastated. >>>The truly sad thing is that I'm too focussed on the first >>>solution to work out a faster way of doing it. >>> >>>Here's the problem: >>>Got a large file (>1Mb) containing binary data. >>>Bytes are in the correct order. >>>Each byte is in low-endian order (lsb first). >>>Want write an output file with bytes in the same order, but with >>> each byte big-endianed. >>>
It seems to me that the fastest way to solve this problem would be with a transform, i.e. tr or y, operator. That way all the computatiomal work could be done by perl at compile time. It might be interesting to write a perl script to generate the two strings used by the tr operator >>>Sounds simple. So I broke out my old friend Bit::Vector, and >>>here's what I did (surrounding stuffs snipped): >>> >>>while (read LL, my $b, 1) { >>> my $h = unpack "H*", $b; >>> my $b_h = Bit::Vector -> new_Hex(8,$h) -> to_Bin; >>> my $r = reverse($b_h); >>> print OF pack "H*", Bit::Vector -> new_Bin(8,$r) -> to_Hex; >>> } >>> >>>LL is my filehandle, $b is current byte, OF is output filehandle, >>>$r the byte in the order I want it for output. >>> >>>Now I realise that the cause of my woe is that I'm using >>>bit::vector to do something that its really overqualified >>>for, and also that >>>I convert to and from binary and hex unecessarily. I can probably >>>skip packing and unpacking, probably not use Bit::Vector, and >>>have a much faster solution - but nothing I've tried has >>>worked. I also >>>failed spectacularly to use Bit::Vector's Reverse method. >>>Thinking it was clever, I also tried doing things with >>>bitwise & and | to >>>map bytes into the other endian form. No joy. >>> >>>SO - given I know somebody out there has done this before, >>>and that you all are laughing at the type changes going on in >>>the code above, can somebody take me out of my misery and >>>suggest improvements so I don't have to spend my weekend >>>feeling like I've allowed VB to do a >>>better job than Perl? > > Try this: > > use strict; > > # create a byte conversion hash - one time so cheap > > for (0 .. 255) { > my $b = 0; > for (my $ii = 0; $ii < 8; $ii++) { > if ($_ & (1 << $ii)) { > $b |= (0x80 >> $ii); > } > } > $hash{$_} = $b; > } > > # open files > > open LL .... > binmode LL; > open FF .... > binmode FF; > > # convert each byte using hash > > while (read LL, my $b, 1) { > print OF $hash{$_}; > } > > close LL; > close FF; > > > -- > ,-/- __ _ _ $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 > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs