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
Related
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);
I am new to Qt.I am working on finger print madoule with this document. I want to send my data to serial port in this format:
I wrote my code in this format, but I think my data has mistake, because this code turn on the LED in some device:
QByteArray ba;
ba.resize(24);
ba[0]=0x55;
ba[1]=0xAA;
ba[2]=0x24;
ba[3]=0x01;
ba[4]=0x01;
ba[5]=0x00;
ba[6]=0x00;
ba[7]=0x00;
ba[8]=0x00;
ba[9]=0x00;
ba[10]=0x00;
ba[11]=0x00;
ba[12]=0x00;
ba[13]=0x00;
ba[14]=0x00;
ba[15]=0x00;
ba[16]=0x00;
ba[17]=0x00;
ba[18]=0x00;
ba[19]=0x00;
ba[20]=0x00;
ba[21]=0x00;
ba[22]=0x27;
ba[23]=0x01;
p->writedata(ba);
Is this data correct?
You're just copying a drawing into code. It won't work without understanding what the drawing means. You seem to miss that:
The LEN field seems to be a little-endian integer that gives the number of bytes in the DATA field - perhaps it's the number of bytes that carry useful information if the packet has a fixed size.
The CKS field seems to be a checksum of some sort. You need to calculate it based on the contents of the packet. The protocol documentation should indicate whether it's across the entire packet or not, and how to compute the value.
It seems like you are talking to a fingerprint identification module like FPM-1502, SM-12, ADST11SD300/310 or similar. If so, then you could obtain a valid command packet as follows:
QByteArray cmdPacket(quint16 cmd, const char *data, int size) {
Q_ASSERT(size <= 16);
QByteArray result(24, '\0');
QDataStream s(&result, QIODevice::WriteOnly);
s.setByteOrder(QDataStream::LittleEndian);
s << quint16(0xAA55) << cmd << quint16(size);
s.writeRawData(data, size);
s.skipRawData(22 - s.device()->pos());
quint16 sum = 0;
for (int i = 0; i < 22; ++i)
sum += result[i];
s << sum;
qDebug() << result.toHex();
return result;
}
QByteArray cmdPacket(quint16 cmd, const QByteArray& data) {
return cmdPacket(cmd, data.data(), data.size());
}
The command packet to turn the sensor led on/off can be obtained as follows:
QByteArray cmdSensorLed(bool on) {
char data[2] = {'\0', '\0'};
if (on) data[0] = 1;
return cmdPacket(0x124, data, sizeof(data));
}
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.
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.
As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.
The q_size is 0x2500 (or the other endianness order - 0x0025).
What's the problem here ? I'm pretty sure q_size holds the data I need.
Something like this should work, using a data stream to read from the buffer:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
The toInt method parses a int if the QByteArray contains a string with digits. You want to interpret the raw bits as an integer. I don't think there is a method for that in QByteArray, so you'll have to construct the value yourself from the single bytes. Probably something like this will work:
int size = (static_cast<unsigned int>(q_size[0]) & 0xFF) << 8
+ (static_cast<unsigned int>(q_size[1]) & 0xFF);
(Or the other way around, depending on Endianness)
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
bool ok;
q_size.toHex().toInt(&ok, 16);
works for me
I had great problems in converting serial data (QByteArray) to integer which was meant to be used as the value for a Progress Bar, but solved it in a very simple way:
QByteArray data = serial->readall();
QString data2 = tr(data); //converted the byte array to a string
ui->QProgressBar->setValue(data2.toUInt()); //converted the string to an unmarked integer..
This works for me:
QByteArray array2;
array2.reserve(4);
array2[0] = data[1];
array2[1] = data[2];
array2[2] = data[3];
array2[3] = data[4];
memcpy(&blockSize, array2, sizeof(int));
data is a qbytearray, from index = 1 to 4 are array integer.
Create a QDataStream that operates on your QByteArray. Documentation is here
Try toInt(bool *ok = Q_NULLPTR, int base = 10) const method of QByteArray Class.
QByteArray Documentatio: http://doc.qt.io/qt-5/QByteArray.html