Ed Chester <[EMAIL PROTECTED]> [2003:09:19:11:15:38+0100] scribed:
<snip />

> > 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. 
> > 
> > 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. 
<snip />

Is this what you want?

====================  BEGIN  ====================
#! /usr/bin/perl -w

use strict;

# Determine the endianness of your system
my $is_little_endian = unpack( 'c', pack( 's', 1 ) );
print $is_little_endian ? "little" : "BIG", " endian\n";

# Try different size chunk's
my $chunk = 32768;
my $infile = shift || "/home/mds/binary";
my $outfile = "./test.out";
-s $outfile && unlink $outfile;

open IN, "$infile"
        or die "\n\tERROR: Cannot read \'$infile\' : $? : $! \n\n";
binmode(IN);
open OUT, ">>$outfile"
        or die "\n\tERROR: Cannot create \'$outfile\' : $? : $! \n\n";
while (read IN, my $b, $chunk) {
   print OUT pack('H*', unpack( 'h*', $b ))
}
close IN;
close OUT;

exit 0;
====================   END   ====================

# ls -al
total 2088
drwxr-sr-x    3 mds      mds          4096 Sep 19 22:52 .
drwxr-sr-x   12 mds      mds          4096 Sep 19 17:03 ..
-rwxr-xr-x    1 mds      mds       1037480 Jul 18 09:51 test.in
-rw-r--r--    1 mds      mds       1037480 Sep 19 22:52 test.out

# time ~/endian.plx ./test.in
little endian

real    0m0.338s
user    0m0.130s
sys     0m0.030s


hth

-- 
Best Regards,

mds
mds resource
877.596.8237
-
Dare to fix things before they break . . .
-
Our capacity for understanding is inversely proportional to how much
we think we know.  The more I know, the more I know I don't know . . .
--

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to