Results 1 to 4 of 4

Thread: Byte shifting in C

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Byte shifting in C

    Hi,

    I have a QByteArray with 4 bytes in that I want to put in an unsigned int with C, sounds simple but ive spent hours messing around and it wont work

    Heres an example of what im trying to do, say i have 4 hex bytes 0xFFAD32FF in a QByteArray as data[0] = 0xFF data[1] = 0xAD data[2] = 0x32 data[3] =0xFF and this wants to go into an uint to give the equivalent decimal value of 4289540863.

    Im using x86 hardware, so little endian is in use, so I think im correct in saying i have to put the bytes from the array in reverse order? so the unsigned int will look like 0xFF 0x32 0xAD 0xFF in memory is that correct?

    Heres my code:
    Qt Code:
    1. QByteArray input;
    2. // test number
    3. input[0] = 0xFF
    4. input[1] = 0xAD
    5. input[2] = 0x32
    6. input[3] = 0xFF
    7.  
    8. // Convert to uint
    9. result = input[3];
    10. result = result << 8 | input[2];
    11. result = result << 8 | input[1];
    12. result = result << 8 | input[0];
    To copy to clipboard, switch view to plain text mode 

    This gives me a result of 4294967295 instead of 4289540863.

    Please can someone point me in the right direction, ive spent a crazy amount of time trying to fix it

  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: Byte shifting in C

    I'd try something similar to
    Qt Code:
    1. memcpy(&result, input.constData(), sizeof(result));
    To copy to clipboard, switch view to plain text mode 
    If you really want to, you can swap the byte order before reading from the array or after writing to the destination variable.

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Byte shifting in C

    Thanks very much, that seems to work correctly if i reverse the order of the byte array before calling memcpy().

  4. #4
    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: Byte shifting in C

    You can also use things such as ntohl() and htonl() to do the conversion. You'd get platform independency for free.

Similar Threads

  1. Two Byte character Support
    By rajveer in forum General Programming
    Replies: 10
    Last Post: 25th October 2008, 01:29
  2. Replies: 2
    Last Post: 4th August 2008, 09:14
  3. convert string to byte values
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 15:06
  4. Can QByteArray takes place of BYTE?
    By hiuao in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2007, 12:49

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.