in reply to Re: Writing Out Binary Files
in thread Writing Out Binary Files

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";

Replies are listed 'Best First'.
Re: Re: Re: Writing Out Binary Files
by Thelonius (Priest) on May 08, 2003 at 16:28 UTC
    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?

      I agree... upon further study pack("u") is not
      the option. However I still need to take the
      ASCII example(above a few postings back).
      Reason (unfortunate one): the boss wants it that way....
      I appreciate your patience.....
Re: Re: Re: Writing Out Binary Files
by iburrell (Chaplain) on May 08, 2003 at 16:54 UTC
    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>;
      Excellent! We are getting even closer!
      Unfortunately knowing very little about binary
      formats(being a novice at all of this),
      I would say lets go for the most efficient and
      compact binary format; so I will defer to your wisdom
      in that area.
      Again I appreciate all the help!