user12979
user12979

Reputation: 101

Understanding this code(writing hex direct to file- midi)

I found this code,

ofstream myfile;
myfile.open ("output.midi",ios::binary);
char buffer[44] = {0x4D,0x54,0x68,0x64,0x00,0x00,0x00,0x06,0x00,0x01,0x00,0x01,0x00,0x80,0x4D,0x54,0x72,0x6B,0x00,0x00,0x00,0x16,0x80,0x00,0x90,0x3C,0x60,0x81,0x00,0x3E,0x60,0x81,0x00,0x40,0x60,0x81,0x00,0xB0,0x7B,0x00,0x00,0xFF,0x2F,0x00};
myfile.write(buffer,44);

How do I go about understanding this, I can see that the first 2 bytes = MZ a PE header signature, what about the other hex values though, how can one just write hex to a file though, I guess these hex values are from a hex dump or is there a way to manually predict what hex values(apart from the header) to write.

I am trying to understand file formats, bmp, jpeg, exe, wav etc - in this case midi.

You can create a bmp from pure code for example, you need to know the header file format and then just create an buffer array of pixel values and use fopen() fwrite().

How about understanding other file formats such as EXE, I take it EXE is unique in the sense that it's compiled of functions/variables and not just a file of pixel, or sound values?

Upvotes: 0

Views: 496

Answers (2)

Raxvan
Raxvan

Reputation: 6505

Each midi has a header that is specific to midi file format. It will not be the same as a bmp format so for each case you have read a documentation for that file. For example for a tga file format you can find some info here. In your case for MIDI you can find more info here

The code you found is just a quick hack that writes the header, in general structures are defined for each header so that others can better understand the meaning of each byte:

For example this is header used to read tga files:

struct TGAHeader
{
    unsigned char       descriptionlen;
    unsigned char       cmaptype;
    unsigned char       imagetype;
    unsigned short      cmapstart;
    unsigned short      cmapentries;
    unsigned char       cmapbits;
    unsigned short      xoffset;
    unsigned short      yoffset;
    unsigned short      width;
    unsigned short      height;
    unsigned char       bpp;
    unsigned char       attrib;
};

In c++ you have to take great care on how you read this structure, because of alignment problems that you might have so in this case here is how you would read it:

TGAHeader header;
fread(&header.descriptionlen,sizeof(header.descriptionlen),1,file);
//and so on for each header member.

Upvotes: 2

Saddam Abu Ghaida
Saddam Abu Ghaida

Reputation: 6739

For any file there is a structure to be able to read from the applications, basically you can write everything to file directly if you know the structure of the file, i did it for office documnets where i needed to construct the documnet my self because i needed to Integrate Rights managemnet system, and i wrote it also PDF for the same. as fas as i know that for common mime type you can find tons of papers discussing the file structure.

for midi format refer to the following document http://faydoc.tripod.com/formats/mid.htm

Upvotes: 0

Related Questions