c++ – Trouble reading integer metadata from file
Trying to read the file size data from a bitmap file. I know that I have the offset (0x02) correct and can find the correct file size in a hex editor.
uint32_t getBMPSize(string bmpPath) {
char sizeread[2] = {};
uint32_t size;
ifstream bmpFile;
bmpFile.open(bmpPath);
uint16_t offset = 0x02;
if (bmpFile.is_open()) {
bmpFile.seekg(offset);
bmpFile.read(sizeread, 1);
bmpFile.close();
}
size = *sizeread;
// Convert size from little-endian to big endian
size = (size >> 24) |
((size << 8) & 0x00FF0000) |
((size >> 8) & 0x0000FF00) |
(size << 24);
return size;
}
I am expecting to get (8A 7B 0C 00) returned to sizeread. This should then be converted to a big-endian 32bit unsigned integer as 818058. As it stands now, the function returns 2332033023.
Read more here: Source link
