# Write int to QByteArray

• What I'd like to do is to write an integer value of 1234 to a QByteArray so that the four bytes of the byte array look like:
12 34 00 00

Any help is greatly appreciated,

Thanks,
Harald

• You need to be more precise.

12 34 00 00 could mean:

• first byte is one, second is two...
• first byte is decimal twelve
• first byte is decimal eighteen (twelve in hex)
• first byte is fourty nine (ascii character '1')
• ...

Which one do you want?

• @
unsigned int var = 1234; // important that this is unsigned!
char a = (var >> 24) & 0XFF;
char b = (var >> 16) & 0XFF;
char c = (var >> 8) & 0XFF;
char d = var & 0XFF;
myBytes.append(a);
myBytes.append(b);
myBytes.append(c);
myBytes.append(d);
@

• [quote author="Thomas Zander" date="1368597455"]@
unsigned int var = 1234; // important that this is unsigned!
char a = (var >> 24) & 0XFF;
char b = (var >> 16) & 0XFF;
char c = (var >> 8) & 0XFF;
char d = var & 0XFF;
myBytes.append(a);
myBytes.append(b);
myBytes.append(c);
myBytes.append(d);
@
[/quote]

In this case you serialise int to a byte array. Result will contain something like this:
00 00 04 D2

H.Klingel is that what you want? Or

[quote author="Chris Kawa" date="1368565772"]You need to be more precise.

12 34 00 00 could mean:

• first byte is one, second is two...
• first byte is decimal twelve
• first byte is decimal eighteen (twelve in hex)
• first byte is fourty nine (ascii character '1')
• ...

Which one do you want?[/quote]

• Thanks to all.
What I want is:
the first byte contains 12 (not 0c !)
the second byte is set to 34 so that the first two bytes of my ByteArray are
12 34
Bytes 3 and 4 are set to Null.

• Sorry, but 12 and 0C is actually the same, depending if you're viewing it as decimal or hex :)
So again - 12 decimal(0C hex) or 12 hex (18 decimal)?

• Or tell us how do you want use result array, then we could understand you clearer.

• I am trying to create a RFC 4122 ByteArray that is the same as QUuid("12340000-0000-0000-0000-000000000000").toRfc4122() where quint32 id = 1234 is a unique ID that needs to be converted very fast into such an RFC4122 array.

The hex view of the resulting array is
12 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00

• That encoding makes no sense at all.

It can't even encode QUuid("CAFFEE00-0000-0000-0000-000000000000") which is as much a valid Uuid as the one you provided.

• I am not trying to build a valid Uuid but a way to convert a quint32 so that it can be used as a key in a QHash<Rfc4122ByteArray, data> where most of the other keys are QUuids that are converted with toRfc4122

• when you write;
QUuid(“12340000-0000-0000-0000-000000000000”)

you first have to convert it to a 128 bit value. Which means
0x12, 0x34, 0x00, 0x00 etc etc
Which is the same as
@
QString input= "“12340000-0000-0000-0000-000000000000”;
bool ok;
unsigned int first4Bytes = input.mid(0, 8).toUInt(&ok, 16);
Q_ASSERT(ok);
// code from reply 2 to turn the unsigned int into a bytearray.
@

at which point you will end up with a 128bit (or 16 bytes)

EDIT: missed the above post, I was slow in typing ;)

• How about using qHash(QUuid::create().toRfc4122()) then? At least that will work with all possible values for QUuid.

Since QHash uses the result of calling qHash(...) on the key anyway to find a bucket, using QHash<quint32, data> is the same as using QHash<Rcf4122ByteArray, data>. So don't bother and use a QByteArray as a key.