Frage

I'm only starting in C++ and am having difficulty understanding certain things. I have a MIDI file and I want to retrieve the hexadecimal string, as detailed here .

How can I extract the hexadecimal string of information from a MIDI file that I read into my C++ program?

War es hilfreich?

Lösung

A byte array is a byte array... as far as I know there isn't a specific kind for MIDI data. Not knowing what OS you're working with makes it difficult to say what specific functions to use, but you would just allocate a block of memory, open the file, read the file's data into that memory block, and close the file. From there, you can work with the data in any way you please.

Andere Tipps

main(){

ifstream::pos_type size;
char * memblock;

ifstream file ("Dvorak_NewWorld.mid", ios::in|ios::binary|ios::ate);
ofstream output;
output.open("output.txt");
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the complete file content is in memory" << endl;

    std::string tohexed = toHex(std::string(memblock, size), true);
    output << tohexed << std::endl;
    output.close();
   }

  return 0;
}

string toHex(const string& s, bool upper_case)
{
    ostringstream ret;

    for (string::size_type i = 0; i < s.length(); ++i)
        ret << std::hex << std::setfill('0') << std::setw(2) << (upper_case ? std::uppercase : std::nouppercase) << (int)s[i];

    return ret.str();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top