Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Writing Out Binary Files

by Anonymous Monk
on May 08, 2003 at 13:09 UTC ( [id://256520]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Using Perl; I want to convert ascii data files into binary output files. How do I do this? I have been doing some digging and not found a finite way. Is there a simple module? Any help would be greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: Writing Out Binary Files
by edan (Curate) on May 08, 2003 at 13:14 UTC

    perldoc -f pack ??

    If you would be a bit more specific about what you mean by

    convert ascii data files into binary output files
    you will probably get more helpful responses...

    --
    3dan
Re: Writing Out Binary Files
by LordWeber (Monk) on May 08, 2003 at 15:20 UTC
        OK Here we go.... Hopefully this is exact enough.... Actually the previous post by 3dan was quite helpful.... what i want to do is convert an ascii file of format similiar to :
        126.000000 2003126 0.000000 -3.831776e-02 126.000692 2003126 0.016600 -3.084112e-02 126.001388 2003126 0.033300 7.476636e-03 126.002083 2003126 0.050000 3.644860e-02
        This gets read into an array (1440 lines), then sent to pack. It looks like the encoding (I am using uuencode) takes place just fine, however, getting the original array back from unpack is a different story (I get nothing). I have provided the test code below: I am new at this binary encode/conversion/decode business so any help provided will be greatly appreciated!
        #!/usr/bin/perl -w use strict; use FileHandle; my @data; my $i=0; print "Loading file: data.dat ......\n"; my $infile = "C:/plot/coveCH1.dat"; open(IN, "$infile"); my $line; while (defined($line = <IN>)) { $data[$i]=$line; $i++; } print "$data[1]\n"; my $bin=pack("u", "@data"); my $OUT= new FileHandle; open ($OUT, ">c:/plot/data.bin"); print $OUT $bin; $OUT->close; my @dataU=unpack("u",$bin); print "$dataU[1]\n";
          This still doesn't make sense to me. What pack("u") does is take _binary_ data and encode it as _ASCII_ data so that it can be sent through ASCII-only transport media (such as 7-bit SMTP). You are taking ASCII data and uuencoding it. This accomplishes nothing but making it 33% longer and hard to read.

          When you do an unpack "u", you are only going to get _one_ string out. If you print $dataU[0]; you will see that it contains all the lines from your original file concatenated, with newlines still included.

          You still haven't explained why you want binary. What do you think the advantage is?

          What format of binary file do you want to convert it to? Right now, your code uuencodes the file. I would not call that a binary format and there is no point in encoding the ASCII file that you have. Also, the unpack won't product an array; it will put the original file in the first element of @dataU, $dataU[0].

          Do you want to convert all the numbers into binary floating point values? First, you will need to split the lines into the numbers

          while (defined($line = <IN>)) { push @data, split(' ', $line); } my $string = pack("d*", @data);

          Notice in the above snippet that Perl is not C and doesn't require using array indexes for putting stuff into an array. Perl has the push operator to append to an array. In this case, it pushes the array of values that split returns. Perl can even read the entire file into an array of lines.

          @data = <IN>;
    Re: Writing Out Binary Files
    by BrowserUk (Patriarch) on May 08, 2003 at 17:12 UTC

      Try telling us the what & why of what you are trying to do, rather than the how you are trying to do it.

      Eg. You have a long list of floating point numbers that you wish to send to another machine and that machine expects them to be in binary form.

      Or: You have a long list of numbers that you wish to save to disk, but you would prefer them to be more compact.

      Or: You have a some binary data that you wish to send someone by email, and want to avoid corruption caused by the stripping of the high order bit.

      Each of these is a possible scenario from what you have said and shown so far, but each requires a different solution.

      A clearer question will get you better answers (and ruffle less feathers:). This is little different from what a lot of other people have already said, but maybe it is worded a little more clearly?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        And the selection is:
        I have a long list of numbers that you wish to save to disk,
        but you would prefer them to be more compact.
        Actually every reply so far has been quite helpful!
        Thanks Again!

          In that case, you should probably be looking at the 'F' or 'D' pack/unpack format specifiers depending upon your needs regarding space versus accuracy.


          Examine what is said, not who speaks.
          "Efficiency is intelligent laziness." -David Dunham
          "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: perlquestion [id://256520]
    Approved by Thelonius
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2024-04-23 20:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found