Results 1 to 4 of 4

Thread: Save 4 Integers in a QByteArray without memcpy

  1. #1
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Save 4 Integers in a QByteArray without memcpy

    Hi,

    I got a QByteArray called mBuffer, and I want to put 4 Integers in it.
    I did this with the append function of QByteArray.
    But if I for Example put these values (2,42,7,9224) in it, in my Buffer stays 24279224.
    But I want that 00'00'00'02'00'00'00'2A'00'00'00'07'00'00'24'08 are in my Buffer.
    00'00'00'02 for 2
    00'00'00'2A for 42
    00'00'00'07 for 7
    00'00'24'08 for 9224

    Those 00 stand for each Byte, and I got 4 for each Integer.
    I think oneway would be to use memcpy, but for this I have to convert my Integer in Char[4] array, and then memcpy it to the Buffer. But I hope there is a more proper Solution.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save 4 Integers in a QByteArray without memcpy

    Qt Code:
    1. int values[] = { 2, 42, 7, 9224 };
    2.  
    3. for(int i = 0; i < 4; i++) {
    4. int number = values[i];
    5. data += QByteArray( (const char*) &number, sizeof(int));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Anyway, if you want to store data into a file or something, it's better to use QDataStream because it's independent of the byte order.

  3. #3
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save 4 Integers in a QByteArray without memcpy

    Thank you,
    For my Programm i didnt use the for-thing, i directly took my variable. But this isnt this relevant.


    Qt Code:
    1. mBuffer += QByteArray((const char*)&mSerial,sizeof(int));
    To copy to clipboard, switch view to plain text mode 

    Ehm but I really dont understand how this works.
    with
    Qt Code:
    1. (const char*)&mSerial
    To copy to clipboard, switch view to plain text mode 
    I get a pointer to the adress my integer, right?

    And with QByteArray(const char*,4) I make a QByteArray with 4 Bytes which are taken from the adress on which the pointer shows?

  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: Save 4 Integers in a QByteArray without memcpy

    And with QByteArray(const char*,4) I make a QByteArray with 4 Bytes which are taken from the adress on which the pointer shows?
    Your assessment seems right to me (at least on machines with 4-byte ints).

    The order of the bytes in the QByteArray will depend on the architecture of the machine, may not be the order you are expecting, and may not be portable to other architectures.

Similar Threads

  1. Replies: 11
    Last Post: 3rd April 2012, 04:51
  2. initialize QStringList with integers
    By tommy in forum Newbie
    Replies: 3
    Last Post: 13th May 2009, 06:48
  3. Only integers from QFontMetricsF width
    By StevenB in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2008, 21:59
  4. how to get integers from mysql
    By eleanor in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2007, 16:25
  5. Strings from LineEdit to Integers
    By Misko in forum Newbie
    Replies: 3
    Last Post: 12th August 2007, 11:11

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
  •  
Qt is a trademark of The Qt Company.