how do you know which bytes in the Hex editor match up to what?
-- you do not, when opening a random binary file. you have to know what location has what data, which you do have from the BMP wiki and similar sites because you know its a bmp file. you know that byte 2 is 'M'. you know that the file size is wherever it is, byte 20 or whatever.
the hex editor assists you: it tells you that whatever in hex is whatever in decimal at 2, 4, 8 bytes or even what it is in float/double. But if you are at the wrong location, that assistance is just noise / totally wrong.
you could write a bmp specific hex editor that delimited the fields, but that would only be useful if you hacked on images constantly.
A dead one that I used to use, the AXE, was able to let you define a C-style struct and pattern and it would do that. Maybe something modern has this; I have not needed it and that was from like 1995 era.
Here is an adaptation of the program in Horstmann.
It shows the file details and data.
An improvement would be to read the file header info as the normal struct's and then decipher it. Similarly the breakdown into the rgb details could be done.
Nevertheless, it shows how to decode the info in a simple way.
How would you modify it so you can pass it a filename as an argument?
Would something like, say,
20 21 22 23 24 25 26 27
std::string filename{};
std::cout << "Enter the name of the file. \n: ";
std::getline (cin, filename);
std::fstream stream;
// Open as a binary file
stream.open(filename.c_str(), std::ios::in | std::ios::binary);
@max
Best is to try it! Probably works OK
Keep in mind the filename is not part of the header or the data on the file.
I made a few small changes but this is essentially the same: