MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Opposite of ReadList[ ... ,Byte]?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50347] Re: [mg50318] Opposite of ReadList[ ... ,Byte]?
  • From: Christopher Purcell <chris.purcell at drdc-rddc.gc.ca>
  • Date: Sat, 28 Aug 2004 04:38:00 -0400 (EDT)
  • References: <200408261051.GAA16434@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

John:    I have made some use of binary files with decent success using 
Version 4.2, but after Version 5 came out - my favorite
little binary read/write routine broke and I have not had time to study 
why.
Here are some trivial examples of creating and reading binary files 
with the Utilities`BinaryFiles package which may be instructive.
These work ok for little test cases except for one anomaly, which is 
noted.

(* Generate a binary file containing a header and 256 Random 8 bit 
integers *)

Needs["Utilities`BinaryFiles`"];

header="This is a dummy file header of length 40"
StringLength[header]
data=Table[Random[Integer,{0,255}],{256}] (*
   here are 256 random 8 bit integers *)
stream=OpenWriteBinary["test.bin"];
Length[data]
WriteBinary[stream,header,
     ByteConversion\[Rule](ToBytes[#,StringConvert\[Rule]CString]&)];
  WriteBinary[stream,data,
     ByteConversion\[Rule](ToBytes[#,IntegerConvert\[Rule]Int8]&)];
Close["test.bin"];


(* Read a binary file containing a known header, and 256 integers (8 
bit) *)

stream=OpenReadBinary["test.bin"];
FromCharacterCode[ReadListBinary[stream,Byte,40]]  (*
   read the header *)
ReadListBinary[stream,Byte,1] (*  why did this 0 get inserted? *)
ReadListBinary[stream,Int8] (*
   read back the data *)
Close[stream];

it was necessary to read an extra byte after the header, which gets 
read back as a 0.
I don't know why this extra byte is there. I have tried to repeat this 
demo with the routines in "Experimental`BinaryExport`"
which is supposed to be faster on large data sets, but I could not get 
it to work. Either finger trouble, or Version 5 has broken something.

As for setting Creator and File Type, in Mac OS X, you can do this from 
the terminal, using the SetFile tool, or even from Mathematica, using 
the Run command to invoke the SetFile. Here (with Mac OS 10.3) we set 
the file creator of our file "test.bin" to "boom" and the file type to 
"bust", and then use GetFileInfo to check our assignments. Note you 
need the Developer Tools installed to get these tools.

Run["/Developer/Tools/SetFile -c boom -t bust test.bin"]
Run["/Developer/Tools/GetFileInfo test.bin>junk.log"];
ReadList[" junk.log", Record, RecordSeparators -> {" "}, RecordLists -> 
True] // TableForm

On Classic equipped Macs, you may have to use a tool like ResEdit or 
DropInfo.


Dr Christopher Purcell
Sensors & Actuators Group
DRDC-Atlantic, 9 Grove St., PO Box 1012,
Dartmouth NS B2Y 3Z7 Canada
chris.purcell at drdc-rddc.gc.ca
Tel 902-426-3100 x389 Fax 902-426-9654
AIM/iChatAV:   cffrc
On Aug 26, 2004, at 7:51 AM, John Kiehl wrote:

> I'm working with a very large (16 Meg) AIFF sound file exported from 
> the
> audio software ProTools which has some undocumented header information
> which I need to preserve. I've successfully loaded the file using
> ReadList[...,Byte], modified the desired data Bytes, and now I want to
> write the new data (with the old header info) back out to a new file.
>
> I'm inclined to not use Export[..., AIFF] because I will lose the 
> ProTools
> custom header information.  In lieu of there not being a symmetrically
> conceived WriteList[..., Byte] function, how do I tell Mathematica to
> simply write my list of bytes out to a file?  I'm confused by all the
> reference to NewLine, Strings, Comma Delimited, etc. etc. etc.... (In
> other words, each element of my list is a number with a value between
> 0 and 255 which needs to be written into just one byte of the file.)
>
> Part B of my question:  I'm working on a Macintosh, so how do I give
> the new file a "File Type" and "Creator" identification to match the
> original file?
>
>
> john kiehl
>
>


  • Prev by Date: Re: Re: Publicon problems converting sample document to LaTeX
  • Next by Date: Re: Looking for a "smart" index for a Do-loop (Revised!)
  • Previous by thread: Opposite of ReadList[ ... ,Byte]?
  • Next by thread: Re: Opposite of ReadList[ ... ,Byte]?