简体   繁体   English

以十六进制字符串形式读取MIDI文件:缺少一些信息

[英]Reading in a MIDI file as a hex string: missing some information

I have a MIDI file which I am trying to read as a hex string: in particular I want to input a MIDI file and to have the hex string available for use. 我有一个MIDI文件,我试图将其读取为十六进制字符串:特别是我想输入一个MIDI文件并准备使用该十六进制字符串。 I have the following: 我有以下几点:

ostringstream ss;
char * memblock;
unsigned char x;
std::string hexFile;

ifstream file ("row.mid", ios::binary);
ofstream output;
output.open("output.txt");

while(file >> x){
    ss << hex << setw(2) << setfill('0') << (int) x;
}

hexFile = ss.str();
cout << hexFile; 

When I output hexFile, I get the following (note the white space near the end): 当输出hexFile时,得到以下内容(请注意末尾的空白):

4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 27c000ff2f00

When I view the MIDI in a hex editor, it reads as follows: 当我在十六进制编辑器中查看MIDI时,其内容如下:

4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 0927c000ff2f00

The latter is definitely correct, as confirmed by track size (around the white space I manually inserted, the correct one has a 09 the former lacks). 可以肯定的是,后者是正确的,可以通过磁道大小来确认(我手动插入的空白附近,正确的是前者缺少的09)。

What could have caused this 09 to go missing in my code? 是什么导致该09在我的代码中丢失?

By default ifstream skips whitespace. 默认情况下,ifstream跳过空格。
All you need to do is tell it not to. 您需要做的就是告诉它不要这样做。

ifstream file ("row.mid", ios::binary);
file.unsetf(ios::skipws); //add this line to not skip whitespace

09 is an ANSII code for tabulation character. 09是制表符的ANSII代码。 The default ofstream mode is for text, and that's why the 09 byte is written as the actual tabulation. 默认的ofstream模式用于文本,这就是为什么将09字节写为实际列表的原因。 Try to set ios::binary also for the output file, and it should be fine. 尝试也为输出文件设置ios::binary ,这应该没问题。

output.open("output.txt", ios::binary);

在声明ifstream file()之后添加以下行似乎可以解决问题:

file >> std::noskipws;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM