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 qbytearray to hex array
Forum Updated to NodeBB v4.3 + New Features

how to convert qbytearray to hex array

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.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.
  • L Offline
    L Offline
    Liny
    wrote on 17 Aug 2022, 11:53 last edited by
    #1

    such as :
    qbytearray test;
    test[0] = 0x30;
    test[1] = 0x31;
    qdebug << "test[0] = "<< test[0];
    qdebug << "test[1] = "<< test[1];
    i want qdebug output:
    test[0] = 0x30;
    test[1] = 0x31;
    not
    test[0] = "0x30" or test[0] = "0"
    what should i do to get this result?

    M 1 Reply Last reply 17 Aug 2022, 12:01
    0
    • L Liny
      17 Aug 2022, 11:53

      such as :
      qbytearray test;
      test[0] = 0x30;
      test[1] = 0x31;
      qdebug << "test[0] = "<< test[0];
      qdebug << "test[1] = "<< test[1];
      i want qdebug output:
      test[0] = 0x30;
      test[1] = 0x31;
      not
      test[0] = "0x30" or test[0] = "0"
      what should i do to get this result?

      M Offline
      M Offline
      mpergand
      wrote on 17 Aug 2022, 12:01 last edited by mpergand
      #2

      @Liny said in how to convert qbytearray to hex array:

      test[0] = 0x30;

      0x30 is a string representation of a binary value in hexadecimal.

      [EDIT]
      QByteArray is an array of char.
      So test[31] is printed as ascii char 1

      To print hex values you can do:
      qdebug << hex << "test[1] = "<< test[1];

      L 1 Reply Last reply 18 Aug 2022, 02:05
      3
      • L Offline
        L Offline
        Liny
        wrote on 17 Aug 2022, 12:18 last edited by
        #3

        thanks for your help!

        1 Reply Last reply
        0
        • M mpergand
          17 Aug 2022, 12:01

          @Liny said in how to convert qbytearray to hex array:

          test[0] = 0x30;

          0x30 is a string representation of a binary value in hexadecimal.

          [EDIT]
          QByteArray is an array of char.
          So test[31] is printed as ascii char 1

          To print hex values you can do:
          qdebug << hex << "test[1] = "<< test[1];

          L Offline
          L Offline
          Liny
          wrote on 18 Aug 2022, 02:05 last edited by
          #4

          @mpergand
          i try this :
          qbytearray test;
          test[0] = 0x30;
          test[1] = 0x31;
          qdebug <<"test[0] = "<< hex << test[0];
          qdebug <<"test[1] = "<< hex << test[1];
          but i get test[0] = 0,test[1] = 1;
          why?

          J M 2 Replies Last reply 18 Aug 2022, 08:01
          0
          • L Liny
            18 Aug 2022, 02:05

            @mpergand
            i try this :
            qbytearray test;
            test[0] = 0x30;
            test[1] = 0x31;
            qdebug <<"test[0] = "<< hex << test[0];
            qdebug <<"test[1] = "<< hex << test[1];
            but i get test[0] = 0,test[1] = 1;
            why?

            J Offline
            J Offline
            JonB
            wrote on 18 Aug 2022, 08:01 last edited by
            #5

            @Liny
            Try test.toHex() or text.toHex(':').

            1 Reply Last reply
            1
            • L Liny
              18 Aug 2022, 02:05

              @mpergand
              i try this :
              qbytearray test;
              test[0] = 0x30;
              test[1] = 0x31;
              qdebug <<"test[0] = "<< hex << test[0];
              qdebug <<"test[1] = "<< hex << test[1];
              but i get test[0] = 0,test[1] = 1;
              why?

              M Offline
              M Offline
              mpergand
              wrote on 18 Aug 2022, 10:44 last edited by mpergand
              #6

              @Liny
              I forgot the cast !
              qDebug() <<hex<< "test[1] = "<< (uint8_t)test[1];

              Note the unsigned int.
              casting to int, uint or uint8_t don't print the same result:

              test[0] = 0x90;
              qDebug() <<hex<< "test[0] = "<< (int)test[0] << (uint)test[0]<< (uint8_t)test[0];
              

              test[0] = -70 ffffff90 90

              J 1 Reply Last reply 18 Aug 2022, 14:06
              0
              • M mpergand
                18 Aug 2022, 10:44

                @Liny
                I forgot the cast !
                qDebug() <<hex<< "test[1] = "<< (uint8_t)test[1];

                Note the unsigned int.
                casting to int, uint or uint8_t don't print the same result:

                test[0] = 0x90;
                qDebug() <<hex<< "test[0] = "<< (int)test[0] << (uint)test[0]<< (uint8_t)test[0];
                

                test[0] = -70 ffffff90 90

                J Offline
                J Offline
                JonB
                wrote on 18 Aug 2022, 14:06 last edited by
                #7

                @mpergand said in how to convert qbytearray to hex array:

                (uint8_t)test[0]

                Why the cast, and why does that change it? I haven't looked, but isn't QByteArray base element type uint8_t, or similar?

                M 1 Reply Last reply 18 Aug 2022, 14:16
                0
                • J JonB
                  18 Aug 2022, 14:06

                  @mpergand said in how to convert qbytearray to hex array:

                  (uint8_t)test[0]

                  Why the cast, and why does that change it? I haven't looked, but isn't QByteArray base element type uint8_t, or similar?

                  M Offline
                  M Offline
                  mpergand
                  wrote on 18 Aug 2022, 14:16 last edited by mpergand
                  #8

                  @JonB

                  char at(int i) const
                  char operator[](int i) const

                  they return char, so debug prints ascii char, hence the cast if you want an other type.

                  J 1 Reply Last reply 18 Aug 2022, 14:29
                  1
                  • M mpergand
                    18 Aug 2022, 14:16

                    @JonB

                    char at(int i) const
                    char operator[](int i) const

                    they return char, so debug prints ascii char, hence the cast if you want an other type.

                    J Offline
                    J Offline
                    JonB
                    wrote on 18 Aug 2022, 14:29 last edited by JonB
                    #9

                    @mpergand said in how to convert qbytearray to hex array:

                    they return char

                    Oohhh, I thought it would return uint8_t (or unsigned char)! Why signed?!

                    1 Reply Last reply
                    0
                    • M mpergand referenced this topic on 10 Feb 2023, 13:50

                    1/9

                    17 Aug 2022, 11:53

                    • 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