Results 1 to 4 of 4

Thread: (portion of...) QByteArray to UInt

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question (portion of...) QByteArray to UInt

    Hi,

    I have this QByteArray whose first 2 bytes are, say 0x11 and 0x08.

    I'd like to extract those 2 byte into a quint16.

    I've tried myByteArray.left(2).toUInt() with no success..

    Since these 2 bytes represent a value in little-endian format I must extract them to a quint16 and then qToBigEndian the value.

    Could someone please point me the proper way to do it?

    TIA,
    Pedro.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: (portion of...) QByteArray to UInt

    Qt Code:
    1. uint val;
    2. memcpy(&val, ba.constData(), 2);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. uint val;
    2. val = (ba[1] << 8) | ba[0]; // order reversed, as you wanted it
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following 2 users say thank you to wysota for this useful post:

    pdoria (25th July 2009), tjm (25th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: (portion of...) QByteArray to UInt

    how about this:
    1. extracting the chars seperately with at(int)
    2. anding their appropriate shifted versions
    3. adding the two results



    Qt Code:
    1. char c1 = myByteArray.at(0);
    2. char c2 = myByteArray.at(1);
    3.  
    4. quint16 myuint = 0;
    5. myuint += (c1<<8)&0xff;
    6. myunint += byte2&0xff;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tjm for this useful post:

    pdoria (25th July 2009)

  6. #4
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: (portion of...) QByteArray to UInt

    Excellent! Again in your debt wysota!
    Thank you very much!

Similar Threads

  1. storing integer 4bytes in QByteArray
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 30th November 2008, 11:08
  2. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 22:53
  3. Invite QDateTime to go 8 secs on future uint
    By patrik08 in forum Newbie
    Replies: 4
    Last Post: 7th June 2006, 00:26
  4. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 18:01
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 21:23

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.