Results 1 to 13 of 13

Thread: how to convert an int to QByteArray

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default how to convert an int to QByteArray

    hi
    how to convert an int to QByteArray

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to convert an int to QByteArray

    Here's a quick, untested solution:
    Qt Code:
    1. for(int i = 0; i != sizeof(n); ++i)
    2. {
    3. byteArray.append((char)(n&(0xFF << i) >>i));
    4. }
    To copy to clipboard, switch view to plain text mode 
    n is the integer you want to store in the byte array.

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

    maverick_pol (19th December 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to convert an int to QByteArray

    Notice static methods of QByteArray.
    J-P Nurmi

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

    babu198649 (19th December 2007)

  6. #4
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: how to convert an int to QByteArray

    hi jpn
    always u think the person who ask question is lazy and just want him to search the entire manual

  7. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to convert an int to QByteArray

    Quote Originally Posted by babu198649 View Post
    hi jpn
    always u think the person who ask question is lazy and just want him to search the entire manual
    I don't think so, because there's no way he could've given you the link directly to the static members section in the QByteArray documentation. There's no link to that section.

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to convert an int to QByteArray

    Quote Originally Posted by babu198649 View Post
    always u think the person who ask question is lazy and just want him to search the entire manual
    Huh? Entire manual? Is it so hard to find static members of QByteArray? In the end, by giving hints rather than direct links, you'll get more familiar with the docs and learn searching them by yourself.
    J-P Nurmi

  9. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: how to convert an int to QByteArray

    cool marcel
    i didn,t mean seriously ,in fact the post was useful and i have even thanked him.

    what i meant was he could have(not necessarily) directly pasted this line .
    QByteArray QByteArray::number ( int n, int base = 10 )

  10. #8

    Default Re: how to convert an int to QByteArray

    Quote Originally Posted by marcel View Post
    Here's a quick, untested solution:
    Qt Code:
    1. for(int i = 0; i != sizeof(n); ++i)
    2. {
    3. byteArray.append((char)(n&(0xFF << i) >>i));
    4. }
    To copy to clipboard, switch view to plain text mode 
    n is the integer you want to store in the byte array.
    Dude that's not correct. it should be:

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

  11. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to convert an int to QByteArray

    Quote Originally Posted by babu198649 View Post
    what i meant was he could have(not necessarily) directly pasted this line .
    QByteArray QByteArray::number ( int n, int base = 10 )
    Why not ???
    Check here

  12. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to convert an int to QByteArray

    I am sure that nearly seven years after the original post, this information will prove to be just the answer the original poster was asking for. Don't you guys ever bother to check the post dates before replying to something?

  13. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to convert an int to QByteArray

    Quote Originally Posted by d_stranz View Post
    Don't you guys ever bother to check the post dates before replying to something?
    But, but, but, somebody was wrong(!) on the Internet!!

    Cheers,
    _

  14. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to convert an int to QByteArray

    But, but, but, somebody was wrong(!) on the Internet!!
    No, never happens. Everything on the Internet is true, but some things are more true than others.

  15. #13
    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: how to convert an int to QByteArray

    Quote Originally Posted by d_stranz View Post
    No, never happens. Everything on the Internet is true, but some things are more true than others.
    Qt Code:
    1. bool operator>(bool lhs, bool rhs) { return true; }
    To copy to clipboard, switch view to plain text mode 

    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.


Similar Threads

  1. Convert QPixmap to QByteArray ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2014, 08:23
  2. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53
  3. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01
  4. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23
  5. Convert from QByteArray to String ?
    By probine in forum Newbie
    Replies: 3
    Last Post: 25th March 2006, 15: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.