Joe Carr Joe Carr - 1 year ago 171
C++ Question

QByteArray conversion to char (not char* or constChar*)

I have an issue which has been bugging for over an hour and decided just to ask here. Is there a proper way of converting a QByteArray to a char? I managed to get it working as char* but I need to pass it as a char.

Here is a code bit where I take the qbytearray and convert it to char*:

QByteArray MainWindow::checkSum(QByteArray &b)
qint16 b_len = b.length();

char xor = 0;

for ( int i = 0 ; i < b_len ; i ++ )
xor = xor ^ b[i];

return b;

void MainWindow::on_flipHorzButton_clicked()
// getFirmwareVersion = { 0xe6, 0x05, 0x12, 0x00}
QByteArray str;

char ch = checkSum(str).data();

str[0] =0xaa;
str[1] =0xbb;
str[2] =0xcc;
str[3] =0xdd;
str[4] = ch;
printf("%s", str[4]);
serial.write(str, 5);

Should I try to convert ch to char? If yes, how should I do it?

Answer Source

For some reason I had to rename "xor" to something else.

For your specific problem, you have to change that line :

char ch = checkSum(str).data()[0];

So that it returns the first character of the char* from the QByteArray

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download