C++ 编写二进制转储和从转储写入二进制

C++ Writing Binary Dump and Writing to Binary from Dump

我很困惑,需要一些帮助。 在我的一个程序中,我正在实现一些代码,这样我就可以在 Windows 上执行一个程序,并基本上执行一个 hexdump(你在十六进制编辑器中看到的),我将它转换为二进制文件并将其写入 .txt文档。我最近才弄明白了一半。另一半代码是将已转储为二进制文件的 .txt 文件中的对象重建为原始对象。这通常适用于所有对象,而不仅仅是重建某个特定对象。

转储部分的部分代码:

//Rip Program to Binary

streampos size; //Declare size variable
unsigned char* memblock; //Holds input
ifstream input; //Declare ifstream
ofstream output("output.txt", ios::out|ios::binary); //Specify ofstream
input.open("sparktools.bin", ios::in|ios::binary|ios::ate); //Open input file and move to the end of it

size = input.tellg(); //Store current location as file length
memblock = new unsigned char [size]; //Declare the input holder with array size as length of file
input.seekg (0, ios::beg); //Return to beginning of file

input.read((char*)memblock, size); //Read each character of the input file until end of file

for (int i=0; i<size; i++) //For each character until end of file:
{   
    std::bitset<sizeof(char) * CHAR_BIT> binary(memblock[i]); //Set bitset<1> to essentially convert to a binary char array
    output << binary; //Output from binary variable created
}

input.close();
output.close();
delete[] memblock;

重建部分的部分代码:

//Restore Ripped Binary To Program

int size; //Holds length of input document
unsigned char* memblock; //Holds input
ifstream input; //Declare ifstream
ofstream output("Binary.bin", ios::out|ios::binary); //Specify ofstream
input.open("Binary.txt", ios::in|ios::binary|ios::ate); //Open input file and move to the end of it

size = input.tellg(); //Store current location as file length
input.seekg(0, ios::beg); //Return to beginning of file
memblock = new unsigned char [size]; //Declare the input holder with array size as length of file
input.read((char*)memblock, size); //Read each character of the input file until end of file

for (int i=0; i<size; i++) //For each character until end of file:
{
    output.write((char*) &memblock[i], size); //Write each char from the input array one at a time until end of file to the output binary

}

input.close();
output.close();
delete[] memblock;

我正在测试图像的功能。原始文件大小为 10 KB。转储后,.txt 文件为 76 KB,内容与十六进制编辑器中显示的内容相匹配。

当我从二进制转储重建时,文件大小不会回到 10 KB。当我增加位集编号时,文件大小会增加。但是,在 bitset<1> 处,它保持在 76 KB。根据我的结论,我基本上需要 bitset <0.125> 来将文件大小从 76KB 减少到 10KB,并且数字应该匹配并且它可以简单地从 .bin 重命名为 .whatever-it-originally-was 并正常运行。但是我不认为 bitset 会低于 1。

我试过的代码:

43555 KB: output.write((char*)&memblock[i], size);
0 KB: output.write((char*)memblock[i], size);
43555 KB: output.write((const char*)&memblock[i], size);
43555 KB: output.write(reinterpret_cast<const char*>(&memblock[i]), size);
43555 KB: output.write(reinterpret_cast<char*>(&memblock[i]), size);
76 KB: output << memblock[i];

当我做这些简单的检查时:

cout << memblock[i];
cout << size;

我看到所有输入都已读入并且与文件中的内容相同。大小变量在 77296 处是正确的。

通过一些猜测,我认为我需要对 bitset 做一些事情,或者手动将位从位转换回字节,转换,然后传递该值以完成此操作。我是在正确的道路上还是我的代码中有 better/easier way/something 错误?在此先感谢您的帮助!

据我所知,问题是位集转储中的位顺序,第一个代码片段的磁盘转储:

11001010

意味着第一位是字节中最重要的一位,因此要对其进行解码,您应该在循环中执行如下操作(已测试):

unsigned char *p = memblock;
//For each character until end of file:
for (int i=0; i<size/8; i++)  
{
    uint8_t byte = 0;
    for (int j = 7; j >= 0; j--) 
        if (*p++ == '1')
            byte |= (1 << j);

    output.write((char*) &byte, 1);
}

请注意,您循环 size/8 因为每 8 个字符编码一个字节,并且内部循环从 7 迭代到 0 以反映第一位更重要的事实。