Results 1 to 17 of 17

Thread: converting QByteArray to unsigned short

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

  2. #2
    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 

    :-)

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

    sattu (28th September 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


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

    sattu (28th September 2010)

  6. #4
    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.

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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
    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,

  9. #7
    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.

  10. #8
    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?

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


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

    sattu (28th September 2010)

  13. #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,

  14. #11
    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.

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