Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Manipulation binary files

Reply
Thread Tools

Manipulation binary files

 
 
Chris
Guest
Posts: n/a
 
      10-19-2003
Dear All,

I'm trying to use perl in a way I am not usd to, and coming unstuck

I have a binary file of unsigned byte values, and I wish to convert these
into a .wav file, so that I can use the soundcard DAC to generate the
waveform the binary file describes (cool huh? Cheap signal generator

The problem is that the wav file format needs to have all all sorts of stuff
munged into the header, including strings "RIFF" "WAVE" and "fmt " and
values that describe other paramters. SOme of these data types are 32 bit,
some are 16 some are 8.

In perl how do i ...

a) write to a file in binary mode. I have tried using binmode(FH) before the
writing beging, but it seems to make little difference

b) write strings in binary mode. I dont really have to do write 0x52 0x49
0x46 0x46 if there is a way to write "RIFF" and have perl do the donkey work

c) Declare the widths of my variables. I need to be able to assign a
variable with a value, and output all 4 bytes of that value to the file in
binary mode.

I'm guessing that all this is very easy, and I'm just looking in the wrong
place

Cheers

Chris


 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      10-19-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Chris" <(E-Mail Removed)> wrote in
news:cSvkb.498$(E-Mail Removed):

> I have a binary file of unsigned byte values, and I wish to convert
> these into a .wav file, so that I can use the soundcard DAC to
> generate the waveform the binary file describes (cool huh? Cheap
> signal generator
>
> The problem is that the wav file format needs to have all all sorts of
> stuff munged into the header, including strings "RIFF" "WAVE" and "fmt
> " and values that describe other paramters. SOme of these data types
> are 32 bit, some are 16 some are 8.
>
> In perl how do i ...
>
> a) write to a file in binary mode. I have tried using binmode(FH)
> before the writing beging, but it seems to make little difference


To write to a binary file, you use "print", just like writing to a text
file. You should use binmode on the file handle beforehand, yes.


> b) write strings in binary mode. I dont really have to do write 0x52
> 0x49 0x46 0x46 if there is a way to write "RIFF" and have perl do the
> donkey work


Yes: print FH "RIFF";


> c) Declare the widths of my variables. I need to be able to assign a
> variable with a value, and output all 4 bytes of that value to the
> file in binary mode.


You should look into the 'pack' operator. It's a pain to use (imho), but
this is exactly what it does: convert numeric variables into binary
strings, suitable for printing to your file.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP5KSV2PeouIeTNHoEQJRCQCfUaNmZpfUwSNWUXoNZMPYzk aLoUYAoKvD
RaVBrEksKxoWpV73CyDPS3H7
=IPZU
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-19-2003
"Eric J. Roode" wrote:
>
> "Chris" <(E-Mail Removed)> wrote in
> news:cSvkb.498$(E-Mail Removed):
> >
> > a) write to a file in binary mode. I have tried using binmode(FH)
> > before the writing beging, but it seems to make little difference

>
> To write to a binary file, you use "print", just like writing to a text
> file. You should use binmode on the file handle beforehand, yes.
>
> > b) write strings in binary mode. I dont really have to do write 0x52
> > 0x49 0x46 0x46 if there is a way to write "RIFF" and have perl do the
> > donkey work

>
> Yes: print FH "RIFF";


And you may want to turn on autoflush for your output file handle. Have
a look in perlvar.pod for "OUTPUT_AUTOFLUSH".


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-23-2003
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Eric J. Roode" wrote:
> >
> > "Chris" <(E-Mail Removed)> wrote in
> > news:cSvkb.498$(E-Mail Removed):
> > >
> > > a) write to a file in binary mode. I have tried using binmode(FH)
> > > before the writing beging, but it seems to make little difference

> >
> > To write to a binary file, you use "print", just like writing to a text
> > file. You should use binmode on the file handle beforehand, yes.
> >
> > > b) write strings in binary mode. I dont really have to do write 0x52
> > > 0x49 0x46 0x46 if there is a way to write "RIFF" and have perl do the
> > > donkey work

> >
> > Yes: print FH "RIFF";

>
> And you may want to turn on autoflush for your output file handle. Have
> a look in perlvar.pod for "OUTPUT_AUTOFLUSH".


How would that make a difference? If the file is a normal disk file,
I don't see the use of autoflush.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: working with binary files/extract png from a binary file Jim Ruby 6 12-24-2013 08:09 AM
Bit Manipulation, Binary Files, Math nguser3552 C++ 3 04-02-2007 04:11 AM
Binary file manipulation Monty Perl Misc 15 04-13-2006 12:37 PM
RE: Binary number manipulation SBrunning@trisystems.co.uk Python 5 12-07-2003 07:37 PM
Binary number manipulation Matthew A. Berglund Python 1 11-28-2003 09:21 PM



Advertisments