View Full Version : Converting some element of QByteArray to Integer

12th April 2014, 19:19
I'm trying to get the width of a bitmap from it's header. I open the bmp with QFile and with that i fill the QByteArray with file.readAll() func.
The main concept was that i convert the QBA to Hexadecimal , and then convert the proper 4 bytest to integer.
What I'm asking is, that is there any other possible ways to convert the 4 bytes of the QBA to integer?

12th April 2014, 20:29
No, "parsing" the content is the only way if you need to work with the data directly.
If you load the image using QImage you can of course get the width from there.


12th April 2014, 20:43
Well, i have to solve this with the method i have mentioned (QFile). I hoped there is another way, but thank you for the answer anyways.

12th April 2014, 20:57
You can use QImageReader (http://qt-project.org/doc/qt-4.8/qimagereader.html#size) class to read image size without actually loading it's contents.

12th April 2014, 21:33
you do not have to do any conversion, just access the data with the right pointer.

First you might be aware that you have a header which preceed the data.
After having loaded the whole dataset, you might read the header and extract at least the number of pixels and the offset of the pixel data within the file.

QByteArray myBmp;
// read the file content into the byte array
int dataCount; // number of pixels, extracted from header
int dataOffset; // data offset, extracted from header
// initialize both values
//Now get an integer pointer onto the data:
int* pData = reinterpret_cast<int*>(myBmp.data())
// and access the data
for ( int i = 0; i < dataCount; ++i ) { // do it for each pixel
pData[ dataOffset + i] ... // pData points to the pixel number i and returns an integer

12th April 2014, 21:53
You do need to be aware of byte order, compression, whether the bitmap file is indexed or not etc.