C++ - Reading number of bits per pixel from BMP file -


i trying number of bits per pixel in bmp file. according wikipedia, supposed @ 28th byte. after reading file:

// przejscie bajtu pod ktorym zapisana jest liczba bitow na pixel         plik.seekg(28, ios::beg);          // read number of bytes used per pixel         int liczbabitow;         plik.read((char*)&liczbabitow, 2);          cout << "liczba bitow " << liczbabitow << endl; 

but liczbabitow (variable supposed hold number of bits per pixel value) -859045864. don't know comes from... i'm pretty lost.

any ideas?

-859045864 can represented in hexadecimal 0xcccc0018.

reading second byte gives 0x0018 = 24bpp.

what happening here, liczbabitow being initialized 0xcccccccc; while plik.read writing lower 16 bits , leaving upper 16 bits unchanged. changing line should fix issue:

int liczbabitow = 0;

though, this, it's best use datatype matches data:

int16_t liczbabitow = 0;

this can found in <cstdint>.