Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved int QByteArray array

    General and Desktop
    2
    2
    66
    Loading More Posts
    • 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.
    • S
      shokarta last edited by shokarta

      Dear all,

      So far i have used this:

      QByteArray writeData_green;
      writeData_green.resize(5);
      writeData_green[0] = 0x05;
      …
      

      but now i need to convert int which will be provided as variable, so lets say:

      int green = 150;
      QByteArray writeData_green;
      writeData_green.resize(5);
      writeData_green[0] = green.toHex();
      …
      

      and I need to write it with "0x" prefix and the HEX value to be in uppercases, so for example 0x8A

      how to do this?

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @shokarta last edited by

        @shokarta
        Use something like:

        writeData_green.setNum(green, 16);
        qDebug() << writeData_green.toHex();
        

        You will have to call QByteArray::toUpper() to get the uppercase letters, and you will have to insert your own 0x prefix, to get a final result of 0x8A. See https://stackoverflow.com/a/36603575/489865 for an example.

        1 Reply Last reply Reply Quote 1
        • JonB
          JonB @shokarta last edited by

          @shokarta
          Use something like:

          writeData_green.setNum(green, 16);
          qDebug() << writeData_green.toHex();
          

          You will have to call QByteArray::toUpper() to get the uppercase letters, and you will have to insert your own 0x prefix, to get a final result of 0x8A. See https://stackoverflow.com/a/36603575/489865 for an example.

          1 Reply Last reply Reply Quote 1
          • First post
            Last post