Courtney, the key to solving your problem is to understand the format of the binary file. If it's a common numerical analysis format, you just might get lucky and find a module on CPAN that will do the work for you.

If not, then you'll need to read the file in and deal with it byte by byte.

#!/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;
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:

din@foobar $ emacs tfile.pl & din@foobar $ chmod +x tfile.pl din@foobar $ ./tfile.pl din@foobar $


Good luck, and welcome to the Monastery! :D

UPDATE: added caveat about null bytes, and comments. UPDATE2: Changed $b to $byt (as per Albannach) so as not to use special sorting variable $b.

Don Wilde
"There's more than one level to any answer."

In reply to Re: Converting Files by samizdat
in thread Converting Files by Courtney1306

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.