QBuffer writes bytes at the start of QByteArray rather than the end - qt

I have the following code:
QByteArray ba; // Declare Byte array
ba.clear(); // Clear it
ba.append(80, 0x00); // Append 80 bytes of 0x00
quint32 Count = 2; // The number we want to append to the byte array
QBuffer tempBuffer(&ba); // We use temporary buffer to conveniently put integers and floats into byte-array
tempBuffer.open(QIODevice::WriteOnly);
Count = qToLittleEndian(Count); // Make sure our number is little Endian
tempBuffer.write((char*)&Count, sizeof(quint32)); // Write the number to byte array
When I print to console the content of my byte array:
qDebug() << "ba: " << ba.toHex();
The console prints:
ba: "0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
As can be seen above, the 2 which is of type quint32, is correctly represented by the little Endian hex value of 0x02000000, however, it is added at the start of the byte array rather than the end. How can I append my value to the end of the byte array?

Open the buffer in append mode instead of writeonly:
tempBuffer.open(QIODevice::Append);

Related

How can i convert a QByteArray into a hex string?

I have the blow QByteArray.
QByteArray ba;
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
I have really no idea how to convert this QByteArray into resulted string which have "01100007", which i would use the QRegExp for pattern matching on this string?
First of all, the QByteArray does not contain "hex values", it contains bytes (as it's name implies). Number can be "hex" only when it is printed as text.
Your code should be:
QByteArray ba(4, 0); // array length 4, filled with 0
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
Anyway, to convert a QByteArray to a hex string, you got lucky: just use QByteArray::toHex() method!
QByteArray ba_as_hex_string = ba.toHex();
Note that it returns 8-bit text, but you can just assign it to a QString without worrying much about encodings, since it is pure ASCII. If you want upper case A-F in your hexadecimal numbers instead of the default a-f, you can use QByteArray::toUpper() to convert the case.
QString has following contructor:
constructor QString(const QByteArray &ba)
But note that an octal number is preceeded by 0 in c++, so some of your values are deciamal, some octal, none of them are hex.

Qt parse string of undefined size from a binary data stream

I have a binary data stream which contains data that should be interpreted as a Qstring. Starting from the third byte. Here is how the package is generated (on a client).
QByteArray package;
package.append( QByteArray::fromHex("0002") ); // First two bytes
package.append( "filename.txt" ); // String of undefined size
package.append( QByteArray::fromHex("00")); // End of string
The decoding is done on a different machine (server). I would like to get a Qstring of value "filename.txt" from the QByteArray package without relying on the size of the string (since the server doesn't have that information) but on the string terminator 00. How can this be achieved?
Since this decoding will be done on a different machine, how should the raw data be generated on the client to avoid problems with endianess?
You should wrap the QByteArray in a QDataStream so you can specify the endianess explicitly and make use of the stream operators
QByteArray package;
QDataStream stream(package, QIODevice::WriteOnly);
stream.setByteOrder( QDataStream::BigEndian);
stream << static_cast<quint16>(0x0002); // First two bytes
stream << "filename.txt"; // String of undefined size
// no need to write terminating 0 because data stream will prepend length
then you can read in the other direction:
QByteArray package;
QDataStream stream(package, QIODevice::WriteOnly);
stream.setByteOrder( QDataStream::BigEndian);
quint16 id;
stream >> id; // First two bytes
char* filename;
stream >> filename; // String of undefined size
QString file = QString.fromLatin1(filename);
delete[] filename; //cleanup
or you can pass a QString to the stream in the first place and not need to deal with the char array:
QByteArray package;
QDataStream stream(package, QIODevice::WriteOnly);
stream.setByteOrder( QDataStream::BigEndian);
stream << static_cast<quint16>(0x0002); // First two bytes
stream << QStringLiteral("filename.txt"); // String of undefined size
note that this will write in utf16 meaning it is unicode enabled
the serialization format is documented at http://qt-project.org/doc/qt-5.0/qtcore/datastreamformat.html

QBitArray size stays zero after trying to fill it with stream operator

Why does the size of QBitArray stay zero? I am using Qt 5.0.2.
QByteArray bytes(4,'b'); // four bytes
QBitArray bits;
QDataStream stream(&bytes, QIODevice::ReadWrite);
stream >> bits;
qDebug() << bytes.size() << bits.size();
When reading from a QDataStream it expects the data to be in a certain format, as described at http://qt-project.org/doc/qt-5.0/qtcore/datastreamformat.html. In the case of streaming QBitArray this is:
The array size (quint32)
The array bits, i.e. (size + 7)/8 bytes
If this is not the case the operation will fail - you can check QDataStream::status() to detect errors.

How can I convert integer to qbitarray?

How can I get QBitArray from qint value? I need to change some bits in number, so I want to use QBitArray for it.
QBitArray's '>>' operator expects bitset size as the first 4 bytes, so JustMaximumPower's snipper won't work. Correct data stream should look like this:
QBitArray bits;
quint32 size = 32;
quint32 value = 1337;
QByteArray data;
QDataStream stream(data, QIODevice::ReadWrite);
stream << size << value;
stream.device()->seek(0);
stream >> bits;
Actually I don't find QBitArray very useful. You could try std::vector<bool> for variable size or std::bitset for fixed size (both from STL library).
std::bitset<32> bits(1337);
bits[0] = 1;
Not testet but should work:
qint value = 1337;
QBitArray bits;
QDataStream stream;
stream << value;
bits << stream;
If you want to just change some bits, QBitArray is overkill.
int bitmask = 1 << 3; // let's change 4th bit
bitmask = 0x55555555; // or perhaps all odd bits
int number = 12345678;
number |= bitmask; // set to 1s
number &= ~bitmask; // set to 0s
number ^= bitmask; // negate what's already there

Convert ByteArray to Integer in Flex

Can someone shed some lights on how to convert ByteArray into int?
Thanks,
So to get a flavor of doing this, you can try this bit of code:
function test(){
var bytes:ByteArray = new ByteArray();
bytes.writeInt(0x00DDAA99); //create my byte array with int 14527129
bytes.position = 0; //move the postion to the start
var newInt:int = bytes.readInt(); //read the bytes from starting position
trace("new num: "+newInt); //print out the number
}
This code will first create a byte array and put an int into it. This is presumably where you need your code to start. This then makes the assumption that there are 4 bytes to read after the starting position which I have set to 0. It then reads the 4 bytes off the byte array into the queue. Note that if you do not have 4 bytes in your ByteArray or your position is not set correctly, your code will fail. Make sure you add the checks for those scenarios.
This code also assumes that the byte array is Big Endian. Make sure that if you have a byte array from another system, that you know which endian-ness the int value has. Change the endian value on your byte array if needed.

Resources