PDA

View Full Version : how to convert an int to QByteArray



babu198649
19th December 2007, 12:55
hi
how to convert an int to QByteArray

marcel
19th December 2007, 13:12
Here's a quick, untested solution:


for(int i = 0; i != sizeof(n); ++i)
{
byteArray.append((char)(n&(0xFF << i) >>i));
}

n is the integer you want to store in the byte array.

jpn
19th December 2007, 13:26
Notice static methods of QByteArray.

babu198649
19th December 2007, 13:52
hi jpn
always u think the person who ask question is lazy and just want him to search the entire manual;)

marcel
19th December 2007, 13:54
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.

jpn
19th December 2007, 14:00
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.

babu198649
19th December 2007, 14:19
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 )

wanderameise
17th September 2014, 14:45
Here's a quick, untested solution:


for(int i = 0; i != sizeof(n); ++i)
{
byteArray.append((char)(n&(0xFF << i) >>i));
}

n is the integer you want to store in the byte array.

Dude that's not correct. it should be:



for(int i = 0; i != sizeof(n); ++i)
{
byteArray.append((char)((n & (0xFF << (i*8))) >> (i*8)));
}

aamer4yu
17th September 2014, 16:04
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 (http://qt-project.org/doc/qt-5/qbytearray.html#number) :)

d_stranz
17th September 2014, 18:00
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?

anda_skoa
18th September 2014, 08:44
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,
_

d_stranz
18th September 2014, 17:53
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.

wysota
19th September 2014, 09:47
No, never happens. Everything on the Internet is true, but some things are more true than others.


bool operator>(bool lhs, bool rhs) { return true; }

;)