Results 1 to 17 of 17

Thread: converting QByteArray to unsigned short

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default converting QByteArray to unsigned short

    Hi everyone!
    This is sattu again. Can anyone tell me how to convert Unsigned Short to QByteArray?
    I don't have any problem in conversion from QByteArray to Unsigned Short, but i also want the reverse conversion which i'm unable to do.
    Can any one please help me?




    With Regards,

  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: converting QByteArray to unsigned short

    What exactly do you mean by "conversion"? Do you want to copy the internal representation of an unsigned short to a byte array or do you want to set the value of the unsigned short in the array or something else?
    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. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by wysota View Post
    What exactly do you mean by "conversion"? Do you want to copy the internal representation of an unsigned short to a byte array or do you want to set the value of the unsigned short in the array or something else?
    By this i mean that if i have got an ushort variable, say crc, where crc=11500, then i should be able to copy that to a qbytearray, like:
    ushort crc = 11500;
    QByteArray byte;
    byte += crc;

    I did it this way. The hexadecimal equivalent of 11500 is 2CEC. So, when i am adding crc to byte, byte is taking only EC and ommiting the first two 2C. Well that is obvoius because qbytearray stores byte by byte, and at a time it can take only FF(255). So thats why it's taking only EC not 2C.
    So, is there any way of doing this?

  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    sattu,

    Why not use QVector<quint16>? What is it you are trying to accomplish?

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

    sattu (28th September 2010)

  6. #5
    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: converting QByteArray to unsigned short

    Qt Code:
    1. QByteArray ba((const char*)&crc, sizeof(ushort));
    To copy to clipboard, switch view to plain text mode 

    Just beware it is extremly unportable.
    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.


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

    sattu (28th September 2010)

  8. #6
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QByteArray ba((const char*)&crc, sizeof(ushort));
    To copy to clipboard, switch view to plain text mode 

    Just beware it is extremly unportable.
    Hi Wysota!
    Thanks for replying, i tried it the way you have posted. But the same problem is coming. it is taking only EC and leaving the rest. Can you suggest any other way of doing this?

  9. #7
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by Timoteo View Post
    sattu,

    Why not use QVector<quint16>? What is it you are trying to accomplish?
    hi Timotoe,
    Actually i'm passing some data from server to client. And data passing is possible only through qbytearray or char* way. But the crc of data that i'm calculating is of unsigned short type. But ultimately i have to pass it along with data. That's why i asked this question. So can you suggest me any way?

  10. #8
    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: converting QByteArray to unsigned short

    Dudes... really...

    Qt Code:
    1. QString::number(ushortNumber, 16)
    To copy to clipboard, switch view to plain text mode 

    :-)

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

    sattu (28th September 2010)

  12. #9
    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: converting QByteArray to unsigned short

    This will make it a string ("2CEC") and not a value ("\x2C\xEC"). I don't know if that's expected but if so then the question was not asked very precisely.
    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.


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

    sattu (28th September 2010)

  14. #10
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by tbscope View Post
    Dudes... really...

    Qt Code:
    1. QString::number(ushortNumber, 16)
    To copy to clipboard, switch view to plain text mode 

    :-)

    Thanks a lot tbscope for this post.
    It's working fine. But well, one more question. here i have taken crc=11500(2cec in hex). Here 2C is one byte and EC is another byte. But when i'm storing it in a QByteArray using the code you gave, it is taking 4 bytes instead of 2 bytes. I mean to say it is storing 2,C,E,C in 4 separate boxes(bytes). Now, it is working fine. But is there any way to put this data inside 2bytes of array rather then 4 bytes(which it is normally taking). Sorry to disturb you, but i would be much thankful if you could help me.


    With regards,

  15. #11
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by wysota View Post
    This will make it a string ("2CEC") and not a value ("\x2C\xEC"). I don't know if that's expected but if so then the question was not asked very precisely.
    Hi wysota!
    You are right, actually i wanted value, not string. But that's not a problem, in the code that tbscope gave, i used QByteArray instead of QString and it is working fine. But the only issue is of memory.

  16. #12
    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: converting QByteArray to unsigned short

    Quote Originally Posted by sattu View Post
    But that's not a problem, in the code that tbscope gave, i used QByteArray instead of QString and it is working fine. But the only issue is of memory.
    You are wrong. The code stores it as a string and not as a value, that's why it's taking four bytes ('2', 'C', 'E', 'C') and not two. The method I gave you should be working fine, maybe you just check it wrong. The code I gave you works just fine.

    Qt Code:
    1. #include <QtDebug>
    2. #include <QByteArray>
    3.  
    4. int main(){
    5. unsigned short val = 0x2CEC;
    6. QByteArray ba((const char*)&val, sizeof(val));
    7. qDebug() << "size:" << ba.size();
    8. qDebug() << "hexdump:" << ba.toHex();
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    yields:
    text Code:
    1. $ ./ba
    2. size: 2
    3. hexdump: "ec2c"
    To copy to clipboard, switch view to plain text mode 

    Note the byte order is reversed (my machine is little endian), hence the unportability I mentioned!
    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.


  17. #13
    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: converting QByteArray to unsigned short

    Quote Originally Posted by sattu View Post
    it is taking 4 bytes instead of 2 bytes.
    I missunderstood. Just ignore my post.

  18. #14
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by wysota View Post
    You are wrong. The code stores it as a string and not as a value, that's why
    Qt Code:
    1. #include <QtDebug>
    2. #include <QByteArray>
    3. int main(){
    4. unsigned short val = 0x2CEC;
    5. QByteArray ba((const char*)&val, sizeof(val));
    6. qDebug() << "size:" << ba.size();
    7. qDebug() << "hexdump:" << ba.toHex();
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    !
    Oh yes, you are right. Actually i had checked it wrongly in the locals and watchers section. It is working as you have told. Well exactly speaking even the reverse order is not a problem. we have to send it from the server side in a qbytearray, that's it. But now the problem is how to convert it back to ushort(in the client side)? I have tried it with .toUShort, but it's not working, as the crc is in hexadecimal form. Let me see, if it's possible, otherwise i will have to trouble you again.

    many many thanks for helping me,

    With regards,

  19. #15
    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: converting QByteArray to unsigned short

    Qt Code:
    1. ushort theNewShort = 0;
    2. memcpy(&theNewShort, ba.data(), sizeof(ushort));
    To copy to clipboard, switch view to plain text mode 

    Conditions: the bytearray contains only those two bytes.

  20. #16
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: converting QByteArray to unsigned short

    Quote Originally Posted by tbscope View Post
    Qt Code:
    1. ushort theNewShort = 0;
    2. memcpy(&theNewShort, ba.data(), sizeof(ushort));
    To copy to clipboard, switch view to plain text mode 

    Conditions: the bytearray contains only those two bytes.
    Thanks tbscope, it's working great. i'm able to do both type of conversion.
    but if i use this code to put an ushort into a qbytearray: QByteArray::number(crc, 16); as you had suggested earlier, then how to retrieve back the ushort from the qbytearray again?

  21. #17
    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: converting QByteArray to unsigned short

    In this situation you can use QByteArray::toUShort() with 16 as the base.
    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.


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

    sattu (28th September 2010)

Similar Threads

  1. QByteArray can not support unsigned char?
    By olosie in forum Newbie
    Replies: 2
    Last Post: 31st July 2010, 10:02
  2. Replies: 5
    Last Post: 9th April 2007, 14:26
  3. Tiff in unsigned short
    By spawnwj in forum Qt Programming
    Replies: 14
    Last Post: 26th July 2006, 07:41
  4. converting string to unsigned integer
    By mgurbuz in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2006, 09:46
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.