473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

?"struct" workaround

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
4 7540
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static...
6
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
10
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
20
by: Francine.Neary | last post by:
People seem to have different views as to where the C reserved word "struct" comes from. One explanation is that it is a shortening of "structure", and another is that it is an acronym for "single...
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.