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
QtWS25 Last Chance

how to convert qbytearray to hex array

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.6k Views
  • 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 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
    0
    • L Liny

      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 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
      3
      • L Offline
        L Offline
        Liny
        wrote on last edited by
        #3

        thanks for your help!

        1 Reply Last reply
        0
        • M mpergand

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

          JonBJ M 2 Replies Last reply
          0
          • L Liny

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • L Liny

              @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 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

              JonBJ 1 Reply Last reply
              0
              • M mpergand

                @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

                JonBJ Online
                JonBJ Online
                JonB
                wrote on 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
                0
                • JonBJ JonB

                  @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 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.

                  JonBJ 1 Reply Last reply
                  1
                  • M mpergand

                    @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.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on 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

                    • Login

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