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. Qt4.8 on VxWorks// Converting QString to unsigned char

Qt4.8 on VxWorks// Converting QString to unsigned char

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k 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.
  • Q Offline
    Q Offline
    Qt_Bg
    wrote on last edited by
    #1

    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.

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

      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.

      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