Results 1 to 7 of 7

Thread: QByteArray conversions/manipulation.... HOW TO DO IT?

  1. #1
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Lightbulb QByteArray conversions/manipulation.... HOW TO DO IT?

    i got several questions regarding QByteArray conversion and manipulation...

    1. how do i convert QByteArray to QBitArray and vice versa? following code should put bytearray to bitarray... is it OK? how it is other way? only change of operator <<?

    code Code:
    1. QBitArray bita;
    2. QByteArray byta;
    3. QDataStream bytads(&byta, QIODevice::ReadWrite);
    4.  
    5. operator>>(bytads,bita);
    To copy to clipboard, switch view to plain text mode 

    2. how do I write/read bytearray from sharedmemory? following code should read 8 bytes form shared memory into bytearray... how to write it?

    code Code:
    1. sharedmem.lock();
    2. QByteArray memar=QByteArray((char*)sharedmem.constData(), 8);
    3. sharedmem.unlock();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QByteArray conversions/manipulation.... HOW TO DO IT?

    just thought on conversion... can I do it this way?

    QVariant(some_qbytearray).toBitArray

    and

    QVariant(some_qbitarray).toByteArray

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QByteArray conversions/manipulation.... HOW TO DO IT?

    The main question is:
    How do you convert
    101011100101001001110
    To an array of bytes?

    Note that a bit array is something completely different than a byte array.
    The bit array is mainly used for setting flags.

  4. #4
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QByteArray conversions/manipulation.... HOW TO DO IT?

    Note that a bit array is something completely different than a byte array.
    The bit array is mainly used for setting flags.


    yes, but i got data from socket as bytearray...
    with QByteArray.mid(x, 1) i will extract some byte and i need to change specific bits on that byte.

    How to do that?

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QByteArray conversions/manipulation.... HOW TO DO IT?

    Just use bitwise operators.

    Either shift to the left or right, use AND, OR, XOR etc...

    Example:

    Qt Code:
    1. unsigned char byte1 = 4; // This is the binary number: 00000100
    2. unsigned char byte2 = 2; // This is the binary number: 00000010
    3.  
    4. usigned char byte1and2 = byte1 & byte2; // This is the binary number: 00000000
    5. usigned char byte1or2 = byte1 | byte2; // This is the binary number: 000001100
    6. etc...
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to tbscope for this useful post:

    daemonna (8th September 2010)

  7. #6
    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: QByteArray conversions/manipulation.... HOW TO DO IT?

    Quote Originally Posted by daemonna View Post
    with QByteArray.mid(x, 1) i will extract some byte and i need to change specific bits on that byte.

    How to do that?
    You don't need QBitArray for that. Just access the data you need and set respective bits using regular C/C++ bit operators. Transforming QByteArray to QBitArray and then back would just give you needless overhead.
    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.


  8. The following user says thank you to wysota for this useful post:

    daemonna (8th September 2010)

  9. #7
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QByteArray conversions/manipulation.... HOW TO DO IT?

    To set a bit:

    byte |= 1 << bitnum

    To clear a bit:

    byte &= ~(1 << bitnum)

  10. The following 2 users say thank you to MTK358 for this useful post:

    daemonna (8th September 2010), IsaacN (21st September 2011)

Similar Threads

  1. Text Manipulation.
    By suneel1310 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2010, 10:07
  2. Replies: 4
    Last Post: 8th May 2010, 11:40
  3. Background manipulation of QPlainTextEdit
    By djjkotze in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 13:34
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 11:21

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.