Results 1 to 5 of 5

Thread: Extracting int values from QByteArray

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Extracting int values from QByteArray

    Hello!
    I've got a problem that is most likely an easy fix for a lot of you but I've been sitting here with this for more than two hours now so I'm reaching out to ya'll! =)

    I have used the sreader example of the QSerialDevice to get receive some data from a microcontroller so I have a QByteArray with five bytes in it.
    Now I simply want to get the decimal representation of the five bytes (0-255) in an integer array or separate ints.
    I could describe to you the methods I tried to use so far but I don't see the point as none of them worked.

    Have a good one, people!
    /Tottish

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting int values from QByteArray


  3. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting int values from QByteArray

    Hmm, I'm not sure what to make of that. The most popular solution seems to be using QDataStream but isn't that kind of a detour? Why can't I just extract a decimal representation from the ByteArray?
    Something like this:

    QByteArray ba = MyDevice->read(readBytes)
    int myInt = ba[1]

    The closest that I've come is to get a 1-128 positive and negative (signed 8-bit?) but that is with quite ugly code and of course I could write an algorithm to convert this to a 0-255 representation but surely there is some really simple syntax to fix this???

    Regards
    /Tottish

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Extracting int values from QByteArray

    You want to access the bytes individually?
    Qt Code:
    1. QByteArray ba(10, '\xaa');
    2. qDebug() << "Byte is "<< static_cast<quint8>(ba[5]);
    To copy to clipboard, switch view to plain text mode 

  5. The following 4 users say thank you to ChrisW67 for this useful post:

    AUDI_ENG (8th July 2014), Miss Engineer (27th May 2014), TheIndependentAquarius (18th March 2016), Tottish (7th April 2010)

  6. #5
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting int values from QByteArray

    Thanks ChrisW67!

    It was the static_cast<quint8> that was the missing piece in my puzzle!

    Thanks a lot! I owe you one! =)
    Have a wonderful day, people!
    /Tottish

Similar Threads

  1. Extracting QT translations from sources
    By mcosta in forum Installation and Deployment
    Replies: 3
    Last Post: 26th April 2010, 17:28
  2. Replies: 3
    Last Post: 4th December 2009, 08:52
  3. Extracting text from QTableWidgetItem
    By bizmopeen in forum Newbie
    Replies: 3
    Last Post: 1st September 2009, 17:28
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. Extracting text from QDomNodes
    By Matt Smith in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2007, 20:27

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.