Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding binary conversions
    primarykey
    data
    text
    <p>I'm writing a resource file which I want to insert a bunch of data from various common files such as .JPG, .BMP (for example) and I want it to be in binary. I'm going to code something to retrieve these data later on organized by index, and this is what I got so far:</p> <pre><code>float randomValue = 23.14f; ofstream fileWriter; fileWriter.open("myFile.dat", ios::binary); fileWriter.write((char*)&amp;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*)&amp;retrieveValue, sizeof(retrieveValue)); fileReader.close(); cout &lt;&lt; retrieveValue &lt;&lt; endl; //This gives me exactly the 23.14 I wanted, perfect! </code></pre> <p>While this works nicely, I'd like to understand what exactly is happening there. I'm converting the address of <em>randomValue</em> to char*, and writing the values in this address to the file?</p> <p>I'm curious also because I need to do this for an array, and I can't do this:</p> <pre><code>int* myArray = new int[10]; //fill myArray values with random stuff fileWriter.open("myFile.dat", ios::binary); fileWriter.write((char*)&amp;myArray, sizeof(myArray)); fileWriter.close(); </code></pre> <p>From what I understand, this would just write the first address' value in the file, not all the array. So, for testing, I'm trying to simply convert a variable to a char* which I would write to a file, and convert back to the variable to see if I'm retrieving the values correctly, so I'm with this:</p> <pre><code>int* intArray = new int[10]; for(int i = 0; i &lt; 10; i++) { cout &lt;&lt; &amp;intArray[i]; //the address of each number in my array cout &lt;&lt; intArray[i]; //it's value cout &lt;&lt; reinterpret_cast&lt;char*&gt;(&amp;intArray[i]); //the char* value of each one } </code></pre> <p>But for some reason I don't know, my computer "beeps" when I run this code. During the array, I'm also saving these to a char* and trying to convert back to int, but I'm not getting the results expected, I'm getting some really long values. Something like:</p> <pre><code>float randomValue = 23.14f; char* charValue = reinterpret_cast&lt;char*&gt;(&amp;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 </code></pre> <p>I want to convert charValue back to randomValue, how can I do it?</p> <p><strong>edit</strong>: There's valuable information in the answers below, but they don't solve my (original) problem. I was testing these type of conversions because I'm doing a code that I will pick a bunch of resource files such as BMP, JPG, MP3, and save them in a single .DAT file organized by some criteria I still haven't fully figured out.</p> <p>Later, I am going to use this resource file to read from and load these contents into a program (game) I'm coding.</p> <p>The criteria I am still thinking but I was wondering if it's possible to do something like this:</p> <pre><code>//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 </code></pre> <p>And then in the code that reads the resource file, I want to do something like this (untested):</p> <pre><code>ifstream fileReader; fileReader.open("myFile.DAT", ios::binary); //file check stuff while(!fileReader.eof()) { //Here I'll load int objectID = 0; fileReader((char*)&amp;objectID, 4); //read 4 bytes to fill objectID char objectType[3]; fileReader(&amp;objectType, 3); //read the type so I know which parser use int objectLength = 0; fileReader((char*)&amp;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 } </code></pre> <p>Currently my code is working with the original files (BMP, WAV, etc) and filling them into classes, and I want to know how I can save the data from these files into a binary data file. For example, my class that manages BMP data has this:</p> <pre><code>class FileBMP { public: int imageWidth; int imageHeight; int* imageData; } </code></pre> <p>When I load it, I call:</p> <pre><code>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 &lt; 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; } } </code></pre> <p>So I have this single dimension array containing values in the format of [RGBA] per pixel, which I am using later on for drawing on screen. I want to be able to save just this array in the binary data format that I am planning that I stated above, and then read it and fill this array.</p> <p>I think it's asking too much for a code like this, so I'd like to understand what I need to know to save these values into a binary file and then read back to fill it.</p> <p>Sorry for the long post!</p> <p><strong>edit2:</strong> I solved my problem by making the first edit... thanks for the valuable info, I also got to know what I wanted to!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload