从MIDI文件获取MIDI字节数组

时间:2013-10-22 20:09:30

标签: c++ hex midi

我只是从C ++开始,很难理解某些事情。我有一个MIDI文件,我想检索十六进制字符串,如详细 here

如何从我读入C ++程序的MIDI文件中提取十六进制信息字符串?

2 个答案:

答案 0 :(得分:1)

字节数组是一个字节数组...据我所知,MIDI数据没有特定种类。不知道你正在使用什么操作系统使得很难说出要使用的具体功能,但你只需要分配一块内存,打开文件,将文件的数据读入该内存块,然后关闭文件。从那里,您可以以任何方式处理数据。

答案 1 :(得分:1)

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