0

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!

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by dorien
1

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();
Votes + Comments
His post totally helped me out!
0

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();

Edited by Fbody: n/a

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.