Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++从DUP中写入二进制转储和二进制写入_C++_Binary_Dump_Rebuild - Fatal编程技术网

C++从DUP中写入二进制转储和二进制写入

C++从DUP中写入二进制转储和二进制写入,c++,binary,dump,rebuild,C++,Binary,Dump,Rebuild,我被难住了,需要一些帮助。 在我的一个程序中,我实现了一些代码,这样我就可以在Windows上运行一个程序,基本上执行一个十六进制转储,就像您在十六进制编辑器中看到的那样,在那里我将其转换为二进制并将其写入.txt文档。我最近才弄明白这一半。代码的另一半是将已转储为二进制文件的.txt文件中的对象重建为原始对象。这通常适用于所有对象,而不仅仅是重建一个特定对象 倾倒部件的部分代码: //Rip Program to Binary streampos size; //Declare size v

我被难住了,需要一些帮助。 在我的一个程序中,我实现了一些代码,这样我就可以在Windows上运行一个程序,基本上执行一个十六进制转储,就像您在十六进制编辑器中看到的那样,在那里我将其转换为二进制并将其写入.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,内容与十六进制编辑器中显示的内容匹配

当我从二进制转储中重建时,文件大小不会回到10KB。当我增加位集编号时,文件大小会增加。但是,在位集,它保持在76 KB。从我的结论来看,我基本上需要位集将文件大小从76KB减少到10KB,并且数字应该匹配,并且可以简单地将其从.bin重命名为.which,无论它最初是什么,并正确运行。但是我不认为比特集低于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处是正确的


通过做一些猜测,我认为我需要对位集做一些事情,或者手动将位转换回字节,转换,然后传递该值来完成这项工作。我走的是正确的道路,还是我的代码中有更好/更简单的方法/出错的地方?提前感谢您的帮助

据我所知,问题在于位集转储中的位顺序,即第一段代码中的磁盘转储:

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

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

非常感谢您的响应!我有一种感觉,我必须手动将位转换回字节,但以前从未使用过。当我用你的代码替换我尝试的for循环时,程序运行得非常好。