c++ - 了解二进制转换

标签 c++ binary binaryfiles

我正在编写一个资源文件,我想从各种常见文件(例如 .JPG、.BMP(例如))中插入一堆数据,我希望它是二进制文件。 我将编写一些代码以稍后按索引组织检索这些数据,这就是我到目前为止所得到的:

float randomValue = 23.14f;

ofstream fileWriter;
fileWriter.open("myFile.dat", ios::binary);
fileWriter.write((char*)&randomValue, sizeof(randomValue));
fileWriter.close();
//With this my .dat file, when opened in notepad has "B!¹A" in it

float retrieveValue = 0.0f;

ifstream fileReader;
fileReader.open("myFile.dat", ios::binary);
fileReader.read((char*)&retrieveValue, sizeof(retrieveValue));
fileReader.close();

cout << retrieveValue << endl; //This gives me exactly the 23.14 I wanted, perfect!

虽然这很好用,但我想了解那里到底发生了什么。 我正在将 randomValue 的地址转换为 char*,并将此地址中的值写入文件?

我也很好奇,因为我需要对数组执行此操作,但我不能这样做:

int* myArray = new int[10];
//fill myArray values with random stuff

fileWriter.open("myFile.dat", ios::binary);
fileWriter.write((char*)&myArray, sizeof(myArray));
fileWriter.close();

据我了解,这只会将第一个地址的值写入文件,而不是整个数组。因此,为了进行测试,我试图简单地将变量转换为 char* ,然后将其写入文件,然后转换回变量以查看我是否正确检索了值,所以我接受了:

int* intArray = new int[10];
for(int i = 0; i < 10; i++)
{
    cout << &intArray[i]; //the address of each number in my array
    cout << intArray[i]; //it's value
    cout << reinterpret_cast<char*>(&intArray[i]); //the char* value of each one     
}

但由于某种我不知道的原因,当我运行这段代码时,我的计算机发出“哔哔声”。在阵列期间,我还将这些保存到 char* 并尝试转换回 int,但我没有得到预期的结果,我得到了一些非常长的值。 像这样的东西:

float randomValue = 23.14f;

char* charValue = reinterpret_cast<char*>(&randomValue);
//charValue contains "B!¹A" plus a bunch of other (un-initiallized values?) characters, so I'm guessing the value is correct

//Now I'm here

我想将 charValue 转换回 randomValue,我该怎么做?

编辑:下面的答案中包含有值(value)的信息,但它们没有解决我的(原始)问题。我正在测试这些类型的转换,因为我正在做一个代码,我会选择一堆资源文件,如 BMP、JPG、MP3,并将它们保存在一个单一的 .DAT 文件中,这些文件是按照我还没有完全理解的一些标准组织的想通了。

稍后,我将使用此资源文件读取这些内容并将其加载到我正在编写的程序(游戏)中。

标准我还在考虑,但我想知道是否有可能做这样的事情:

//In my ResourceFile.DAT
[4 bytes = objectID][3 bytes = objectType (WAV, MP3, JPG, BMP, etc)][4 bytes = objectLength][objectLength bytes = actual objectData]
//repeating this until end of file

然后在读取资源文件的代码中,我想做这样的事情(未经测试):

ifstream fileReader;
fileReader.open("myFile.DAT", ios::binary);
//file check stuff
while(!fileReader.eof())
{
    //Here I'll load
    int objectID = 0;
    fileReader((char*)&objectID, 4); //read 4 bytes to fill objectID

    char objectType[3];
    fileReader(&objectType, 3); //read the type so I know which parser use

    int objectLength = 0;
    fileReader((char*)&objectLength, 4); //get the length of the object data

    char* objectData = new char[objectLength];
    fileReader(objectData, objectLength); //fill objectData with the data

    //Here I'll use a parser to fill classes depending on the type etc, and move on to the next obj
}

目前我的代码正在处理原始文件(BMP、WAV 等)并将它们填充到类中,我想知道如何将这些文件中的数据保存到二进制数据文件中。 比如我管理BMP数据的类有这个:

class FileBMP
{
    public:
        int imageWidth;
        int imageHeight;
        int* imageData;
}

当我加载它时,我调用:

void FileBMP::Load(int iwidth, int iheight)
{
    int imageTotalSize = iwidth * iheight * 4;
    imageData = new int[imageTotalSize]; //This will give me 4 times the amount of pixels in the image

    int cPixel = 0;
    while(cPixel < imageTotalSize)
    {
        imageData[cPixel] = 0;     //R value
        imageData[cPixel + 1] = 0; //G value
        imageData[cPixel + 2] = 0; //B value
        imageData[cPixel + 3] = 0; //A value
        cPixel += 4;
    }
} 

所以我有一个包含每个像素 [RGBA] 格式的值的单维数组,稍后我将使用它在屏幕上绘图。 我希望能够以上面提到的我计划的二进制数据格式保存这个数组,然后读取它并填充这个数组。

我认为这样的代码要求过高,所以我想了解将这些值保存到二进制文件中然后读回以填充它需要了解的内容。

抱歉发了这么长的帖子!

edit2:我通过第一次编辑解决了我的问题...感谢您提供的宝贵信息,我也知道了我想要做什么!

最佳答案

通过使用 & 运算符,您将获得指向变量内容的指针(将其视为内存地址)。

float a = 123.45f;
float* p = &a; // now p points to a, i.e. has the memory address to a's contents.
char* c = (char*)&a; // c points to the same memory location, but the code says to treat the contents as char instead of float.

当您为 write() 提供 (char*)&randomValue 时,您只是告诉“获取这个具有 char 数据的内存地址并从那里写入 sizeof(randomValue) 个字符”。您不是在写入地址值本身,而是该内存位置的内容(“原始二进制数据”)。

cout << reinterpret_cast<char*>(&intArray[i]); //the char* value of each one 

在这里您需要提供 char* 类型的数据,以空字符(零)结尾。但是,您提供的是浮点值的原始字节。您的程序可能会在这里崩溃,因为 cout 将输入字符,直到它找到终止符字符——它可能很快就找不到了。

float randomValue = 23.14f;
char* charValue = reinterpret_cast<char*>(&randomValue);

float back = *(float*)charValue;

编辑:要保存二进制数据,您只需提供数据并将其写入()。不要对 ofstream/cout 使用 << 运算符重载。例如:

    int values[3] = { 5, 6, 7 };
struct AnyData
{
   float a;
   int b;
} data;

cout.write((char*)&values, sizeof(int) * 3); // the other two values follow the first one, you can write them all at once.
cout.write((char*)&data, sizeof(data)); // you can also save structs that do not have pointers.

如果您要编写结构,请查看#pragma pack 编译器指令。编译器会将(使用填充)变量对齐到特定大小 (int),这意味着以下结构实际上可能需要 8 个字节:

#pragma pack (push, 1)
struct CouldBeLongerThanYouThink
{
  char a;
  char b;
};
#pragma pack (pop)

此外,不要自己写指针值(如果结构中有指针成员),因为一旦从文件中读回,内存地址将不会指向任何有意义的数据。始终写入数据本身,而不是指针值。

关于c++ - 了解二进制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675408/

相关文章:

c++ - X3 : Is this parser, *(char - eol),消耗所有行?

javascript - 从二进制文件中识别数据类型

curl - 通过 cURL 发布 zip 文件会得到二进制输出

java - 从java中的二进制文件读取Double时得到错误的值

ios - 覆盖 AVMetadataMachineReadableCodeObject 的 stringValue 属性或创建输出二进制数据的替代方法

java - 如何写入/读取表示对象的二进制文件?

c++ - 使用模板调用类的函数

c++ - C: 如何检查 free() 是否释放了内存?

c++ - std::valarray 的 operator* 有什么问题?

http - 是否值得为二进制通信实现一个服务器