You kept saying you wanted to obtain a set of 16 bit integers. You can convert these to a hex string int by int: there is no toHex() method in QList, and there is no QByteArray involved.
There are a lot of ways to do things vaguely like your request but you have to actually understand what you are trying to achieve and know some basic programming.
QList<quint16> result; // A list of 16 bit unsigned integers
for (int i = 0; i < ba.size(); i+=2)
result.append( (static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1)) );
// You now have a list of 16 bit ints like you requested.
// If you wanted only a hex string
for (int i = 0; i < ba.size(); i+=2) {
quint16 r = static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1);
result
+= QString(%1
").arg(r, 4, 16, '0' ); // watch out for byte order if this needs to be portable}
QList<quint16> result; // A list of 16 bit unsigned integers
for (int i = 0; i < ba.size(); i+=2)
result.append( (static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1)) );
// You now have a list of 16 bit ints like you requested.
// If you wanted only a hex string
QString hex;
for (int i = 0; i < ba.size(); i+=2) {
quint16 r = static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1);
result += QString(%1").arg(r, 4, 16, '0' ); // watch out for byte order if this needs to be portable
}
To copy to clipboard, switch view to plain text mode
Given that you already have the data in a buffer, and the result will be exactly the same size as the source data, then you could have done the job in-situ with a bit of thought:
// char *buff
// int numbytes
for (int i = 0; i < numbytes; i+=2) {
unsigned char a = static_cast<unsigned char>( buff[i] );
unsigned char b = static_cast<unsigned char>( buff[i+1] );
buff[i] = a >> 3;
buff[i+1] = a << 5 | b;
}
// char *buff
// int numbytes
for (int i = 0; i < numbytes; i+=2) {
unsigned char a = static_cast<unsigned char>( buff[i] );
unsigned char b = static_cast<unsigned char>( buff[i+1] );
buff[i] = a >> 3;
buff[i+1] = a << 5 | b;
}
To copy to clipboard, switch view to plain text mode
Bookmarks