Results 1 to 3 of 3

Thread: (solved) QByteArray vs ByteBuffer (Java)

  1. #1
    Join Date
    Feb 2016
    Posts
    21
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default (solved) QByteArray vs ByteBuffer (Java)

    Please can anyone help me.

    how to i but a qlonglong to a ByteArray with this Order

    Example:

    In Java:

    Qt Code:
    1. Long n = 60243;
    2. ByteBuffer byteBufferData = ByteBuffer.allocate(16);
    3. byteBufferData.putLong(turnoverCounter);
    To copy to clipboard, switch view to plain text mode 
    I got this Array "0 0 0 0 0 0 -21 83 0 0 0 0 0 0 0 0"
    I only need the first 8 Bytes "0 0 0 0 0 0 -21 83"

    In Cpp (Qt5):

    Qt Code:
    1. qlonglong n = 60243;
    2. for(int i = 0; i != sizeof(n); ++i)
    3. {
    4. ba.append((char)((n & (0xFF << (i*8))) >> (i*8)));
    5. }
    To copy to clipboard, switch view to plain text mode 

    I got the right length but in other order;
    I got "83 -21 0 0 0 0 0 0"

    How can i reverse this? can i put this in the reverse order in the loop? Or is this any problem?


    Thx Chris
    Last edited by ckvsoft; 26th October 2016 at 23:07. Reason: solved

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

    Default Re: QByteArray vs ByteBuffer (Java)

    The Java Virtual Machine is a Big Endian machine, while your real CPU, most likely an x86 variant, is Little Endian.

    You could use qToBigEndian() or similar for the conversion or maybe QDataStream with ByteOrder set to BigEndian for the whole process of creating a byte array.

    Something like this (untested)

    Qt Code:
    1. QDataStream stream(&data, QIODevice::WriteOnly);
    2. stream.setByteOrder(QDataStream::BigEndian);
    3. stream << n;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    21
    Qt products
    Qt5
    Platforms
    Unix/X11

    Lightbulb Re: QByteArray vs ByteBuffer (Java)

    Ohh very very cool

    It works fine.

    I'll use QDataStream


    lg Chris

Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 12:14
  2. Replies: 1
    Last Post: 22nd June 2011, 08:12
  3. Qt and Java
    By gmseed in forum General Discussion
    Replies: 1
    Last Post: 21st November 2009, 11:55
  4. [java] Sax
    By mickey in forum General Discussion
    Replies: 0
    Last Post: 29th September 2009, 09:42
  5. Replies: 9
    Last Post: 25th July 2009, 13: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.