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. Convert unsigned char to QString
Forum Updated to NodeBB v4.3 + New Features

Convert unsigned char to QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.7k 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.
  • A Offline
    A Offline
    aftershocker1
    wrote on 15 Sept 2021, 08:32 last edited by
    #1

    Hello

    Looking for some help. I am trying to convert an unsigned char array to a string or QString. I have tried a few different ways so far with no success or crashing the application when testing.

    The char array need into a string is buf[8] + buf[9] and these contain hex codes (ie f8 + 6f). So I want my string to read "f86f". I didn't think it would be as difficult as it is as it is no problem printing f8 and 6f to the console!

    Thanks in advance!

    J 1 Reply Last reply 15 Sept 2021, 08:58
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 15 Sept 2021, 08:36 last edited by
      #2

      Try something like this:

      const QByteArray data = QByteArray::number(buf);
      const QString string = data.toHex();
      

      brain to terminal, not tested; you'll probably need to tweak it.

      (Z(:^

      1 Reply Last reply
      0
      • A aftershocker1
        15 Sept 2021, 08:32

        Hello

        Looking for some help. I am trying to convert an unsigned char array to a string or QString. I have tried a few different ways so far with no success or crashing the application when testing.

        The char array need into a string is buf[8] + buf[9] and these contain hex codes (ie f8 + 6f). So I want my string to read "f86f". I didn't think it would be as difficult as it is as it is no problem printing f8 and 6f to the console!

        Thanks in advance!

        J Offline
        J Offline
        JonB
        wrote on 15 Sept 2021, 08:58 last edited by JonB
        #3

        @aftershocker1 said in Convert unsigned char to QString:

        The char array need into a string is buf[8] + buf[9] and these contain hex codes (ie f8 + 6f). So I want my string to read "f86f".

        If you really mean this --- the string is to read f86f, i.e. showing the characters/bytes in hex --- you will want to use QString::toLatin1().toHex().

        1 Reply Last reply
        3
        • A Offline
          A Offline
          aftershocker1
          wrote on 15 Sept 2021, 09:46 last edited by
          #4

          I don't think I understood the problem properly in my initial post. The value in buf[9] is 248 (0xF8) and the value in buf[10] is 111 (0x6f) although these values may change(data is coming in from a hid).

          In the console I can get it to print the value in hex format using

          qDebug("%02hhx ", buf[9]);
          qDebug("%02hhx ", buf[10]);
          

          So, what I want to do is use the hex values and turn them into a string(once in a string I have a function to convert the string into binary which enables me to see the error flags from the hid).

          What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?

          S J 2 Replies Last reply 15 Sept 2021, 09:51
          0
          • A aftershocker1
            15 Sept 2021, 09:46

            I don't think I understood the problem properly in my initial post. The value in buf[9] is 248 (0xF8) and the value in buf[10] is 111 (0x6f) although these values may change(data is coming in from a hid).

            In the console I can get it to print the value in hex format using

            qDebug("%02hhx ", buf[9]);
            qDebug("%02hhx ", buf[10]);
            

            So, what I want to do is use the hex values and turn them into a string(once in a string I have a function to convert the string into binary which enables me to see the error flags from the hid).

            What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?

            S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 15 Sept 2021, 09:51 last edited by
            #5

            @aftershocker1 said in Convert unsigned char to QString:

            What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?

            That's what we answered above already.

            But isn't it wasteful for you to convert a number (which is already binary!) to string, then to hex, then to binary again? Why don't you just work on the numbers directly?

            (Z(:^

            A 1 Reply Last reply 15 Sept 2021, 09:57
            2
            • A aftershocker1
              15 Sept 2021, 09:46

              I don't think I understood the problem properly in my initial post. The value in buf[9] is 248 (0xF8) and the value in buf[10] is 111 (0x6f) although these values may change(data is coming in from a hid).

              In the console I can get it to print the value in hex format using

              qDebug("%02hhx ", buf[9]);
              qDebug("%02hhx ", buf[10]);
              

              So, what I want to do is use the hex values and turn them into a string(once in a string I have a function to convert the string into binary which enables me to see the error flags from the hid).

              What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?

              J Offline
              J Offline
              JonB
              wrote on 15 Sept 2021, 09:52 last edited by
              #6

              @aftershocker1
              I already suggested QString::toLatin1().toHex(). Without bothering with a QString why not go straight from a QByteArray (you have bytes) via QByteArray QByteArray::toHex() const?

              1 Reply Last reply
              1
              • S sierdzio
                15 Sept 2021, 09:51

                @aftershocker1 said in Convert unsigned char to QString:

                What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?

                That's what we answered above already.

                But isn't it wasteful for you to convert a number (which is already binary!) to string, then to hex, then to binary again? Why don't you just work on the numbers directly?

                A Offline
                A Offline
                aftershocker1
                wrote on 15 Sept 2021, 09:57 last edited by
                #7

                @sierdzio Good point! I have just got a little lost in the process.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aftershocker1
                  wrote on 15 Sept 2021, 10:48 last edited by aftershocker1
                  #8

                  So the code below why would data = 248 (which is the correct value of buf[9]) and data_as_hex_string = 323438?

                    QByteArray data = QByteArray::number(buf[9]);
                    QString data_as_hex_string = data.toHex();
                  

                  I thought the data_as_hex would be F8?

                  J 1 Reply Last reply 15 Sept 2021, 11:23
                  0
                  • A aftershocker1
                    15 Sept 2021, 10:48

                    So the code below why would data = 248 (which is the correct value of buf[9]) and data_as_hex_string = 323438?

                      QByteArray data = QByteArray::number(buf[9]);
                      QString data_as_hex_string = data.toHex();
                    

                    I thought the data_as_hex would be F8?

                    J Offline
                    J Offline
                    JonB
                    wrote on 15 Sept 2021, 11:23 last edited by JonB
                    #9

                    @aftershocker1
                    My friend, you really need to sort your understanding of bytes, strings & numbers out! :)

                    why would data = 248

                    No! data is set to "248", a string, not a number. Look at https://doc.qt.io/qt-5/qbytearray.html#number

                    Returns a byte array containing the string equivalent of the number n to base base (10 by default).

                    So the string is 3 characters, and the hex string representation of those 3 bytes is "323438".

                    You want something more like:

                    QByteArray data(buf, sizeOfBuffer);    // wrap buffer (or some section of it) in a `QByteArray`
                    QString data_as_hex_string = data.mid(9, 2).toHex();    // pick out bytes [9,10] for conversion to hex
                    
                    1 Reply Last reply
                    3

                    1/9

                    15 Sept 2021, 08:32

                    • Login

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