How does one check for specific values in a QByteArray?
Something like this:
QByteArray example;
//...
example = file.readAll();//fill with data from a file
//...
if(example.mid(0, 2) == 0x0a00)
//do something
The above doesn't work.
I CAN do this but I am currently fixing performance issues so I'd rather not have to convert it to a QString:
QByteArray example;
//...
example = file.readAll();//fill with data from a file
//...
if(example.mid(0, 2).toHex() == "0a00")
//do something
What's the fastest way to check the bytes in a QByteArray based on their hex representation?
You can compare simply like this.
if(example.mid(0, 2) == "\x0a\x00")
Edited:
This is not working. Check below comment.
Related
I'm developing an app with Qt and I have to read frames and check each byte to see if its values are correct.
QSerialPort serial;
QByteArray data = serial.readAll();
I well receive bytes but I don't know how to check if they are well equal to my hex values. I'd like to do something like this:
if (data[0] == 0x02) {
// my code
}
My question will probably be stupid for most of you but I don't find any answer :(
My goal is to save a QAudioRecorder recording into memory. From my research it seems the best way to store the recording is to use a QByteArray. My audio recorder is probed using a QAudioProbe.
From the audioBufferProbed signal I try to append the data to the byte array using this slot method.
QByteArray *byteArr;
void AudioRecorder::processBuffer(const QAudioBuffer &buffer)
{
byteArr->append(buffer.constData<char>());
qDebug() << buffer.byteCount();
qDebug() << byteArr->size();
}
However that doesn't seem to work considering buffer.byteCount(); returns 4092 constantly which seems normal but byteArr->size(); returns weird and irregular increments starting off usually with 2, 4, 6, 7, 189.
The data also usually only ends up being around 18kb in size which also leads me to believe that the data is not being appended into the byte array correctly.
According to the QByteArray::size() docs size() should give how many bytes are in the array. Along with QAudioBuffer::byteCount() which also should give the amount of bytes in the current buffer, shouldn't the full 4092 from the buffer be copied to the array?
I am also open to another solution that doesn't use QByteArray if there is a better way to store the data.
You have a QByteArray pointer, but have not set it to anything.
You need to set it to a QByteArray, in your case you can use QByteArray(const char * data, int size):
byteArr = new QByteArray(buffer.constData<char>(), buffer.byteCount());
But to be honest, I am not sure why you are using a pointer.
You could just do:
QByteArray byteArr; // Your global declaration
:
:
byteArr.append(buffer.constData<char>(), buffer.byteCount());
You might want to try printing like this if you are using binary data...:
qDebug() << byteArr.toHex();
You are using method QByteArray::append which does something else than you expect. This overload of append you are using appends bytes until zero is encountered. This API should be used for c-strings!
So fix code like this:
QByteArray byteArr; // there is no point of create this on heap
byteArr.reserve(8*1024*1024); // reserve 8 MB - it will improve performance
void AudioRecorder::processBuffer(const QAudioBuffer &buffer)
{
byteArr.append(buffer.constData<char>(), buffer.size());
qDebug() << buffer.byteCount();
qDebug() << byteArr.size();
}
I'm a beginner in Qt. Now I want to use Qt5 to send a 9-byte command through uart.
Here is my command:
FFFFFF5550464DAA0E
I want to transfer my command to a Qstring object. When I write my code like this, it tells me the const is too big.
QString str=0xFFFFFF5550464DAA0E;
So I choose an array like this, but it still doesn't work.
char cmd[9]={0xFF,0xFF,0xFF,0x55,0x50,0x46,0x4D,0xAA,0x0E};
for(int i=0;i<9;i++)
{
QString str=cmd[i];
QByteArray outData=str.toLatin1();
int size=outData.size();
outData=myHelper::HexStrToByteArray(str);
size=outData.size();
myCom->write(outData);
}
I also try this which failes again
char cmd[9]={0xFF,0xFF,0xFF,0x55,0x50,0x46,0x4D,0xAA,0x0E};
QString str=cmd;
QByteArray outData=str.toLatin1();
int size=outData.size();
outData=myHelper::HexStrToByteArray(str);
size=outData.size();
myCom->write(outData);
So could anyone tell me how to do this ?
This line of code:
QString str=0xFFFFFF5550464DAA0E;
0xFFFFFF5550464DAA0E is not a string. You're trying to assign a very big constant (9 bytes) number to a string. Note that 0xFF is not a string but a character with ASCII code 0xFF. With your second attempt you're on the right way:
char cmd[9]={0xFF,0xFF,0xFF,0x55,0x50,0x46,0x4D,0xAA,0x0E};
Now you have two options; it depends on what you have to send, 9 bytes or a longer string with that commands represented as a hex string and encoded as ASCII. First case is easier, drop all your code:
QByteArray outData = QByteArray(cmd, sizeof(cmd));
myCom->write(outData);
With this code you won't send a string to your device but 9 bytes (0xFF...0x0E). If you have to send a string then you can do what paxdiablo suggested:
QByteArray outData = QByteArray("\xFF\xFF\xFF\x55\x50\x46\x4D\xAA\x0E", 9);
myCom->write(outData);
Or:
QByteArray outData = QString("0xFF0xFF0xFF0x550x500x460x4D0xAA0x0E")
.toLatin1();
myCom->write(outData);
Or in alternative you can do this:
char cmd[9]={0xFF,0xFF,0xFF,0x55,0x50,0x46,0x4D,0xAA,0x0E};
QByteArray outData = QByteArray(cmd, sizeof(cmd)).toHex();
myCom->write(outData);
Which one is right for you? Well you should clarify your context...
You don't need to mess about with strings and conversions. You can just make the QByteArray directly from the data itself, with a simple one-liner:
QbyteArray data("\xFF\xFF\xFF\x55\x50\x46\x4D\xAA\x0E", 9);
Following that, the statement:
myCom->write(data);
will then output the nine bytes as specified in the string.
I iterate over a QByteArray which contains words. I will compare the array content with a given word (QString).
for(QByteArray::iterator it = content.begin(); it != content.end(); it++){
if(content.at(*it) == word){
...
}
}
the compiler say on line (if(content.at ..)) : invalid conversion from 'char' to 'const char*' [-fpermissive]
how can i compare the values in this case?
Chris
I iterate over an qbytearray which contains words from a file. I will compare each word with a given word.
Thanks for the clarification. In that case1 I would convert the QByteArray to QString and then split it into individual words which can then be trivially compared.
// QByteArray is implicitly convertible to QString
QString allWords = yourByteArray;
// split the string at each whitspace or newline
QStringList aWordlist = allWords.split(QRegExp("[\s\r\n]"), QString::SkipEmptyParts)
for (QStringList::iterator it=aWordlist.begin(); it != aWordlist.end(); ++it)
{
// it points to the next word in the list
if (*it == word)
{
...
}
}
1I'm assuming that you can't change the fact that you receive the file contents as byte array. Otherwise, it would probably be better to open a QFile and read the contents from there.
How can i compare the values in this case?
According to the QString documentation, QString can be compared to QByteArray without iterating. So you could simply say:
QString word("Hello");
QByteArray bytes("hi");
if (word == bytes)
{
...
}
QByteArray contains bytes. QString contains a string, that is a sequence of characters. A single byte cannot be compared to a sequence of bytes.
*it is a byte and you try to compare it to a word(i.e. a sequence of chars). I am not sure what you are trying to do but maybe you should compare content.at(*it) to the first char in the word?
I think I know what you want to do and the problem is not so much with the comparison as with the fact that you have stored your text in a QByteArray rather than a QString, or some kind of container such as a QVector, etc.
You need to look into different ways of reading in data from the QFile class. Check out the docs here:
QFile
QIODevice
I solved the problem: (QString word;)
void MainWindow::startSearching()
{
word = ui->passwordTxt->toPlainText();
string a;
fstream inputFile;
inputFile.open(fileName.data());
while(!inputFile.eof()){
inputFile >> a;
if(a == word.toStdString()){
//anything
break;
}
}
inputFile.close();
}
I'm reading data from a file and trying to display the raw data as 2 digit hex strings.
I'm using the Qt framework, specifically the QTextEdit.
I've tried a bunch of different approaches and have almost accomplished what I want it to do, however it has some unexpected errors I don't know anything about.
Currently this is my implementation:
1) Read in the data:
ifstream file (filePath, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size+1];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
}
2) Create a single QString that will be used (because QTextEdit requires a QString):
QString s;
3) Loop through the array appending each successive character to the QString s.
int count = 0;
for(i=0;i<size;i++)
{
count++;;
s.append(QString::number(memblock[i], 16).toUpper());
s.append("\t");
if (count == 16)
{
s.append("\n");
count -= 16;
}
}
Now this works fine, except when it reaches a character FF, it appears as FFFFFFFFFFFFFFFF
So my main questions are:
Why do only the 'FF' characters appear as 'FFFFFFFFFFFFFFFF' instead?
Is there a way to convert the char data to base 16 strings without using QString::number?
I want this implementation to be as fast as possible, so if something like sprintf could work, please let me know, as I would guess that might be faster that QString::number.
QString can't be used for binary data. You should use QByteArray instead. It can be easily created from char* buffer and can be easily converted to hex string using toHex.
QByteArray array(memblock, size);
textEdit->setText(QString(array.toHex()));
QString::number doesn't have an overload that takes a char, so your input is being promoted to an int; consequently you're seeing the effects of sign extension. You should be seeing similar behavior for any input greater than 0x7F.
Try casting the data prior to calling the function.
s.append(QString::number(static_cast<unsigned char>(memblock[i]), 16).toUpper());