How to convert ’QString 1’ to ’unsigned char’ (solved)
-
i thought using toLocal8bit can convert from QString to unsigned char but i am wrong.
error: cannot convert 'QString' to 'unsigned char' in assignment
@
Brightness0[6] = value.toLatin1();
'QByteArray::operator QNoImplicitBoolCast() const' is privateBrightness0[6] = value.data();
Invalid conversion from 'QChar*' to 'unsigned char'Brightness0[6] = value.toLocal8bit().data();
Invalid conversion from 'QChar*' to 'unsigned char'Brightness0[6] = (unsigned char *)(value);
Invalid cast from type 'QString' to type 'unsigned char'@
@
void MainWindow::SendBrightness()
{
//total 8 bytes,first 6 bytes fixed,7 bytes-value,8 bytes-CRC
unsigned char Brightness0[]= {0x08,0x11,0x01,0x00,0x00,0x00,0x00,0x00};
unsigned char Brightness1[]={0x04,0x12,0x83,'\0'};//append GUI value QString value=ui->BrightnessCombo->currentText(); Brightness0[6] = (value); //!!!!!error //calculate CRC and appending char crc0 = ModBusCRC(Brightness0,sizeof(Brightness0)); Brightness0[7]=(crc0); //send value out serial->write((char *)Brightness0);
}
@
-
Are you trying to assign the whole string, or just one character? (Remember that a string is an array of characters)
Asked another way: Do you want to use "unsigned char" or "unsigned char *"?
-
[quote author="houmingc" date="1420706407"]Trying to assigned one character[/quote]Think of a QString as an array of QChar.
@
Brightness[6] = value[6].toLatin1();
@ -
If value is a string that represents a number, then you'll first want to convert the string to an actual number: use one of the functions that create an unsigned number for you, like toUShort or toUInt. Then, it's a simple cast to put that value into a char (which is now used as a one-byte number, not as a character), which can then be assigned to your array.
So, you end up with something like this:
@
bool ok;
ushort sValue = value.toUShort(&ok);
if (!ok) {
//handle error
}
char cValue = static_cast<char>(sValue);Brightness[6] = cValue;
@However, I think it would be simpler if you replace your combo box with something that actually returns a numeric value to begin with, like a QSpinBox and/or a QSlider.
-
[quote author="houmingc" date="1420710723"]QString value=ui->BrightnessCombo->currentText();
is change to
QString value=ui->BrightnessCombo->currentindex();
to solve the problem[/quote]
That's just wrong too. The last variant returns an integer, and then you put it into a string again. Why? -
Need to add hex value in an array[] and minus the total with 0x100.
Below is the code i scribbled.
Is there an easier way to parse it. Hope to get some feedback before bedtime.@
unsigned char Brightness0[]= {0x08,0x11,0x01,0x00,0x00,0x00,0x00,0x64};
unsigned char EndHexValue =0x100;//convert Hex value to int int EndIntValue=EndHexValue.toUInt(0,16); //Array below is integer Equal of Brightness0 int EqualArray[]; //IntSum to contain total value of integer int IntSum=0; unsigned char HexSum=0; for (int a=0; a<sizeof(Brightness0); a++) { //Convert hex to int and set it into Integer array Equalarray[a]= Brightness0[a].toUInt(0,16); //Integer total value IntSum += EqualArray[a]; } //Integer total value EndIntValue -= intSum; HexSum=HexSum.Num(EndIntValue,16);
@
Edit: merged with your previous thread on the same issue. Please don't start multiple threads on the same problem. Andre