Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to convert ’QString 1’ to ’unsigned char’ (solved)

How to convert ’QString 1’ to ’unsigned char’ (solved)

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 9.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #3

    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.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #4

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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #6

          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.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            houmingc
            wrote on last edited by
            #7

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

            is change to

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

            to solve the problem

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #8

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

              1 Reply Last reply
              0
              • H Offline
                H Offline
                houmingc
                wrote on last edited by
                #9

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #10

                  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?

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    houmingc
                    wrote on last edited by
                    #11

                    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

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      ckakman
                      wrote on last edited by
                      #12

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved