Hi,

I have been reading a number of posts about this, but I have not been able to figure it out.

I want to write hex to a midi file.

The hex code of the midi file looks like this:

4D 54 68 64 00 00 00 06 00 01 00 01 00 80 4D 54 72 6B 00 00 00 16 80 00 90 3C 60 81 00 3E 60 81 00 40 60 81 00 B0 7B 00 00 FF 2F 00

This is confirmed by opening an existing midi file with okteta.


So far I tried:

ofstream myfile;
myfile.open ("output.midi",ios::binary);
myfile << "4D 54 68 64 00 00 00 06 00 01 00 01 00 80 4D 54 72 6B";
myfile << "00 00 00 16 80 00 90 3C 60 81 00 3E 60 81 00 40 60 81 00 B0 7B 00 00 FF 2F 00";
myfile.close();

But this writes it as normal characters. Then I read that << adds formatting, so I should use write. I found this command;

write((char*)&i, sizeof i);

But that's just chinese to me.

How do I implement the write command correctly to write the hex stream. Or just how do write the hex stream.

Immensly gratefull!

Recommended Answers

Well, okteta reads binary data and prints it out in hex format (to be more readable than if you opened a binary file in a text editor). So what you need to write is binary data:
[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);
myfile.close();
[/CODE]

Jump to Post

All 3 Replies

Re: Write hex to a file 80 80

Well, okteta reads binary data and prints it out in hex format (to be more readable than if you opened a binary file in a text editor). So what you need to write is binary data:

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);
myfile.close();
commented: His post totally helped me out! +2
Re: Write hex to a file 80 80

Well, okteta reads binary data and prints it out in hex format (to be more readable than if you opened a binary file in a text editor). So what you need to write is binary data:

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);
myfile.close();

Won't that just output the equivalent values as decimal integers instead of hex integers? Why not just use the hex stream manipulator:

#include <iomanip> //required for access to stream manipulators

const int BUFFER_SIZE = 44;

ofstream myFile;
myFile.open("output.mid", ios::out | ios::binary);

char buffer[BUFFER_SIZE] = 
{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};

for (int i = 0; i < BUFFER_SIZE; ++i) {
  myFile << hex << buffer[i];
  //NOTE: you may need the showbase manipulator and whitespace as well, I don't know exactly how MIDI files work
}

myFile << dec;
myFile.close();
Re: Write hex to a file 80 80

Mike_2000_17's post worked great for me.

Thank you soooo much, this saved me hours of searching. I would never have suspected that I needed to write 0x4D instead of 4D.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.