Results 1 to 6 of 6

Thread: Converting some element of QByteArray to Integer

  1. #1
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows Android

    Default Converting some element of QByteArray to Integer

    Hi
    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?
    Last edited by s3l3ctr; 12th April 2014 at 21:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Converting some element of QByteArray to Integer

    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.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Converting some element of QByteArray to Integer

    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.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Converting some element of QByteArray to Integer

    You can use QImageReader class to read image size without actually loading it's contents.

  5. #5
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [SOLVED]Converting some element of QByteArray to Integer

    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.

    Qt Code:
    1. QByteArray myBmp;
    2. // read the file content into the byte array
    3. int dataCount; // number of pixels, extracted from header
    4. int dataOffset; // data offset, extracted from header
    5. // initialize both values
    6. //Now get an integer pointer onto the data:
    7. int* pData = reinterpret_cast<int*>(myBmp.data())
    8. // and access the data
    9. for ( int i = 0; i < dataCount; ++i ) { // do it for each pixel
    10. pData[ dataOffset + i] ... // pData points to the pixel number i and returns an integer
    11. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,652
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [SOLVED]Converting some element of QByteArray to Integer

    You do need to be aware of byte order, compression, whether the bitmap file is indexed or not etc.

Similar Threads

  1. Replies: 12
    Last Post: 22nd August 2012, 14:45
  2. converting QByteArray to unsigned short
    By sattu in forum Qt Programming
    Replies: 16
    Last Post: 28th September 2010, 13:51
  3. storing integer 4bytes in QByteArray
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 30th November 2008, 10:08
  4. Converting to integer
    By Salazaar in forum Newbie
    Replies: 2
    Last Post: 15th June 2007, 20:11
  5. converting string to unsigned integer
    By mgurbuz in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2006, 09:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.