Re: Binary Data File reading

From: Date: Thu, 10 May 2001 21:14:51 +0000
Subject: Re: Binary Data File reading
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-52352@lists.php.net to get a copy of this message
Try using unpack() --zak ----- Original Message ----- From: "Chris Mattmann" <Chris.Mattmann@jpl.nasa.gov> To: <php-general@lists.php.net> Sent: Thursday, May 10, 2001 2:54 PM Subject: [PHP] Binary Data File reading > Hi there- > > Does anyone have any clue as to what may be causing this problem. I am > > trying to read from a binary data value that has temperature values stored in it > > in short integer format. So basically, for every 2 bytes of binary data, that value > > in short integer is the temperature value. I have written a simple C program to read > > in the values from the out.bin binary data file I have, and echo them back to the console: > > It prints out the correct values, here is the C program that I used: > > #include<stdio.h> > > void main(void) > > { > > FILE *fin; > > short buffer[1]; > > int num_read; > > fin = fopen("out.bin","r"); > > num_read=1; > while(num_read>0) { > num_read = fread(buffer, sizeof(short), 1, fin); > printf("%d,",buffer[0]); > } > > fclose(fin); > return(1); > > } > > I am trying to write a php file to do the exact same thing, except instead of printing > > out the values, I'm going to store them in an array. Anyways, the php script I've written > > looks like this: > > <?php > > $fp = fopen("out.bin","rb"); > > $str = fread($fp,2); > > while($str) > > { > > $str = (int)$str; > > printf("%d,",$str); > > $str = fread($fp,2); > > } > > ?> > > The output I get from the php script is complete garble(a whole bunch of 0's, some 1's, some random > > int values, etc. I know that PHP doesn't exactly have a short int type, i know that > > their integers are actually stored in the C LONG_INT type, which are 8 byte integers. Do > > you think that this may be affecting my script, and if it is, does anyone know of any > > workarounds? Your help would be greatly appreciated. > > Thanks in advance. > > ------------------------------------------------------- > Chris Mattmann > Science Data Management and Archiving > Chris.Mattmann@jpl.nasa.gov > NASA-JPL Pasadena, CA 91109-8099 > Office: 300-324C > Phone: 818-354-8810 > > >

« previous php.general (#52352) next »