in reply to Converting Files
I didn't fully test it beyond sanity check, and if you use this in production, you'll want to add little niceties like not writing the very last comma and ignoring null bytes, but this should take each four bytes of the file, convert them to an integer, and write them out in groups of 16 per line. A Windows-based system might have a little different invocation; this is done on Linux:#!/usr/bin/perl use warnings; use strict; my $buf; my @barray; my $byt; my $acc = 0; # file 'datafile.bin' must exist and be in same directory as program f +ile. open BFILE, "<./datafile.bin" or die "Can't find or open binary data f +ile.\n"; # not necessary on UNIX but helps with line-delineated files on Doze. binmode BFILE; # program will create 'tfile.csv' in same directory open OFILE, ">./tfile.csv" or die "Can't create output file.\n"; while ( read BFILE, $buf, 64 ) { my $bindex = 0; @barray = unpack('C*', $buf); for ( my $batch = 0; $batch < 16; $batch++ ) { $acc = 0; for ( my $i = 0; $i < 4; $i++ ) { if ( exists( $barray[ $bindex ] ) ) { $byt = $barray[ $bindex++ ]; $acc *= 256; $acc += $byt; } } printf OFILE '"' . $acc . "\","; } printf OFILE "\n"; } close BFILE; close OFILE;
din@foobar $ emacs tfile.pl & din@foobar $ chmod +x tfile.pl din@foobar $ ./tfile.pl din@foobar $
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting Files
by Grundle (Scribe) on May 30, 2007 at 15:05 UTC |