473,574 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

?"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 7558
"R. Rajesh Jeba Anbiah" <ng**********@r ediffmail.com> wrote in message
news:ab******** *************** ***@posting.goo gle.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?

--
"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**********@r ediffmail.com> napisal w
wiadomosci news:ab******** *************** ***@posting.goo gle.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?

--
"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**********@re diffmail.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.wa v', 'r');

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

var_dump($fileh eader);

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**********@re diffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<ab******* *************** ****@posting.go ogle.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.c om/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
4741
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 non-integral data member in a class?
6
2377
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
3
14483
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 program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
10
2719
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
28443
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
40458
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
15
2656
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 then adds values to "jd" 3.) in the end it prints values of "jd".
20
1826
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 type representing useful compound types". Does anyone here know the history of the word?
8
2162
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 (where there is an
0
8040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8092
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5610
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3745
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2230
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1051
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.