By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

?"struct" workaround

P: n/a
Recently I've done a project to fiddle WAV (format) files. To read the
headers, I've used something like:

fseek($fp, 40); //Data length is at offset 40
$data_len = fread($fp, 4); //4 bytes

This method is really hectic as you know. By Googling, I couldn't
find any better solution. In C, we can do it easily with struct like:

typedef struct tagFILEHEADER
{
BYTE x[4];
BYTE y[111];
BYTE z;
WORD xx;
BYTE yy;
} FILEHEADER;

So, my question is: Is there any *elegant* way of doing it in PHP?

--
"Silence is the only right answer for many wrong questions" --
G.K.Moopanar, Indian Politician
Email: rrjanbiah-at-Y!com
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...
Recently I've done a project to fiddle WAV (format) files. To read the
headers, I've used something like:

fseek($fp, 40); //Data length is at offset 40
$data_len = fread($fp, 4); //4 bytes

This method is really hectic as you know. By Googling, I couldn't
find any better solution. In C, we can do it easily with struct like:

typedef struct tagFILEHEADER
{
BYTE x[4];
BYTE y[111];
BYTE z;
WORD xx;
BYTE yy;
} FILEHEADER;

So, my question is: Is there any *elegant* way of doing it in PHP?

--
"Silence is the only right answer for many wrong questions" --
G.K.Moopanar, Indian Politician
Email: rrjanbiah-at-Y!com

what was wrong with your example? 2 lines, looked good

also, do you know what a structure really is? its another view at a data set
of bytes. Its a compile thing, when you need to account for every byte.
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2

P: n/a
Nope. Usually I do something like this:

$size = 4 * 3 + 2 * 4; // size of structure
$data = fread($fp, $size);
extract(unpack("Vlen/Vwidth/Vheight/vword4", $data));

Which would yield the variables $len, $width, $height, $word1, $word2,
$word3, $word4.

You have to manually calculate the size of the struct. Would be nice if
there's a PHP function that does that for you based on the unpack format
string.

Uzytkownik "R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> napisal w
wiadomosci news:ab**************************@posting.google.c om...
Recently I've done a project to fiddle WAV (format) files. To read the
headers, I've used something like:

fseek($fp, 40); //Data length is at offset 40
$data_len = fread($fp, 4); //4 bytes

This method is really hectic as you know. By Googling, I couldn't
find any better solution. In C, we can do it easily with struct like:

typedef struct tagFILEHEADER
{
BYTE x[4];
BYTE y[111];
BYTE z;
WORD xx;
BYTE yy;
} FILEHEADER;

So, my question is: Is there any *elegant* way of doing it in PHP?

--
"Silence is the only right answer for many wrong questions" --
G.K.Moopanar, Indian Politician
Email: rrjanbiah-at-Y!com

Jul 17 '05 #3

P: n/a
On 5 Jan 2004 01:25:46 -0800, ng**********@rediffmail.com (R. Rajesh Jeba
Anbiah) wrote:
Recently I've done a project to fiddle WAV (format) files. To read the
headers, I've used something like:

fseek($fp, 40); //Data length is at offset 40
$data_len = fread($fp, 4); //4 bytes

This method is really hectic as you know. By Googling, I couldn't
find any better solution. In C, we can do it easily with struct like:

typedef struct tagFILEHEADER
{
BYTE x[4];
BYTE y[111];
BYTE z;
WORD xx;
BYTE yy;
} FILEHEADER;

So, my question is: Is there any *elegant* way of doing it in PHP?


unpack seems a likely candidate; PHP's version is related but not the same as
the same command in Perl.

http://uk2.php.net/unpack

There's a useful looking link in the user-contributed notes with a fuller
explanation.

For the struct above I think you'd want something like:

<pre>
<?php
$fp = fopen('chord.wav', 'r');

$fileheader = unpack(
"C4x/A111y/Cz/sxx/Cyy",
fread($fp, 4+111+1+2+1)
);

var_dump($fileheader);

fclose($fp);
?>
</pre>

Look at 'pack' for the format strings to use.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4

P: n/a
ng**********@rediffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<ab**************************@posting.google. com>...
Recently I've done a project to fiddle WAV (format) files. To read the
headers, I've used something like:

fseek($fp, 40); //Data length is at offset 40
$data_len = fread($fp, 4); //4 bytes

This method is really hectic as you know. By Googling, I couldn't
find any better solution. In C, we can do it easily with struct like:

typedef struct tagFILEHEADER
{
BYTE x[4];
BYTE y[111];
BYTE z;
WORD xx;
BYTE yy;
} FILEHEADER;

So, my question is: Is there any *elegant* way of doing it in PHP?


Many thanks to all the experts answered in this thread. Yes,
unpack seems to be the right choice. This link is really useful
<http://fooassociates.com/phpfer/html/rn45re878.html>

--
"Silence is the only right answer for many wrong questions" --
G.K.Moopanar, Indian Politician
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.