Qt4.8 on VxWorks// Converting QString to unsigned char
-
I am a newbie to Qt and programming in general and have a question to ask.
I need to execute commands to set display contrast and it has data format as below:
[command][data][Checksum =command +data]
example:
Contrast 100 -> Command : [0x83][0x64][0xE7]Brief explanation: I need to implement something that reads int type of contrast value and convert it to hex value and execute as above command. I converted the int value to QString type of hex value but do not know how to convert QString to char type.
I tried below, but it only prints the fist value. For instance if checksum is E7, it only prints E.
is there a way to convert to char and store E7 instead of just first E?@char checkSum1=QString(Sum).at(0).toLatin1(); // only prints the first value.@
Below are Data structure and what I have tried.
@
union serialData
{
unsigned char bytes[SERIAL_DATA_LENGTH];
struct {
unsigned char command;
unsigned char data;
unsigned char checkSum;} format;
};
void SettingButton::formatSettingValue(AppConstants::SettingType type)
{
QString hexVal,Sum;int value=ui->ValueProgressBar->value(); hexVal.setNum(value,16); hexVal= QString("0x%1").arg(hexVal); int sum= value+131; // 0x83 is 131 in Decimal Sum.setNum(sum,16); Sum= QString("0x%1").arg(Sum); char checkSum1=QString(Sum).at(0).toLatin1(); // only prints the first value. char checkSum2=QString(Sum).at(1).toLatin1();// prints the second value switch (type) { case AppConstants::BRIGHTNESS: serial_data.bytes[0]=0x84; break; case AppConstants::CONTRAST: serial_data.bytes[0]=0x83; break; default: break; } serial_data.bytes[1]=command; serial_data.bytes[2]=checkSum;
}
@Thanks in advance.
-
Hi,
Try
@
QByteArray hex = QByteArray::number(value, 16)
@Here hex will be of size() == 2, where hex.at(0) and hex.at(1) are chars.
See the "function documentation":http://doc.qt.io/qt-5/qbytearray.html#number for more details.
A general note: Qt has en exceptionally good documentation where you can find example code where you need it. Always first check the docs and you may solve your issues much faster.