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 private

    Brightness0[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);             
    

    }

    @


  • Moderators

    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 *"?



  • Trying to assigned one character
    Trying to replace Array[6] with QString value & Array[7] with char value.

    Initially, I tried append to variable Brightness0[] index6 & index7, but not successful.


  • Moderators

    [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();
    @



  • Value of ui->BrightnessCombo is from 0 to 100
    QString value=ui->BrightnessCombo->currentText();

    I need to convert QString to one QChar and insert into index 6, but this is not successful
    @
    Brightness[6] = value.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.



  • QString value=ui->BrightnessCombo->currentText();

    is change to

    QString value=ui->BrightnessCombo->currentindex();

    to solve the problem



  • [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?



  • i need to put it into a unsigned char array bit 6; my code is in the first post :>



  • But... A char is a number, not a string. Why convert a number to a string, and then jump through hoops to get a number again?



  • 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



  • Hi,

    You can replace the for loop with std::accumulate().

    By the way I don't get why there are so many conversions there. The computer doesn't know number bases.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.