Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to convert hex array data to integer ?
Forum Updated to NodeBB v4.3 + New Features

How to convert hex array data to integer ?

Scheduled Pinned Locked Moved Solved C++ Gurus
17 Posts 5 Posters 3.4k Views 2 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 Qt embedded developer

    @JonB

    actually my aim to how to combine this 2 data into one integer value ?

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #4

    @Qt-embedded-developer that depends, what's the high byte, what low ?


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    Q 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @Qt-embedded-developer that depends, what's the high byte, what low ?

      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by
      #5

      @J-Hilk here first byte is high and 2nd byte is low. then how to combine it ?

      J.HilkJ 1 Reply Last reply
      0
      • Q Qt embedded developer

        @J-Hilk here first byte is high and 2nd byte is low. then how to combine it ?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #6

        @Qt-embedded-developer

        int arr [10] ={0xAA, OxBB};
        
        int16_t value = (0xAA << 8) | 0xBB; // (arr[0] << 8) | arr[1]
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • Q Qt embedded developer

          @JonB

          actually my aim to how to combine this 2 data into one integer value ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @Qt-embedded-developer said in How to convert hex array data to integer ?:

          actually my aim to how to combine this 2 data into one integer value ?

          That was not clear (at least, not to me)! :)

          Q 1 Reply Last reply
          0
          • JonBJ JonB

            @Qt-embedded-developer said in How to convert hex array data to integer ?:

            actually my aim to how to combine this 2 data into one integer value ?

            That was not clear (at least, not to me)! :)

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #8

            @JonB actually i face one interview question where they asked to me that

            if you have array which store hex data. where first 2 index show one sensor data. that sensor data you need to store it in one integer value. How to do this ? I hope this is clear. if not then let me know why ?

            JonBJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @JonB actually i face one interview question where they asked to me that

              if you have array which store hex data. where first 2 index show one sensor data. that sensor data you need to store it in one integer value. How to do this ? I hope this is clear. if not then let me know why ?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #9

              @Qt-embedded-developer said in How to convert hex array data to integer ?:

              if you have array which store hex data

              There is no such thing as "hex [integer/numeric] data". A number is a number is a number. Unless you/they meant "a string of hex digits as characters", which you would need to convert to a number first, but I doubt they said that, or at least not as you phrased it.

              J.HilkJ 1 Reply Last reply
              0
              • JonBJ JonB

                @Qt-embedded-developer said in How to convert hex array data to integer ?:

                if you have array which store hex data

                There is no such thing as "hex [integer/numeric] data". A number is a number is a number. Unless you/they meant "a string of hex digits as characters", which you would need to convert to a number first, but I doubt they said that, or at least not as you phrased it.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #10

                @JonB I would have given/phrased the task this way:

                Given an array arr, where each entry has the size of 2 nibble.
                Formulate the most generic way to combine consecutive entries to an integer.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                JonBJ 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @JonB I would have given/phrased the task this way:

                  Given an array arr, where each entry has the size of 2 nibble.
                  Formulate the most generic way to combine consecutive entries to an integer.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #11

                  @J-Hilk
                  If you say so :)

                  1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #12

                    Endianess? The little endian that could, or the big endian that interneted?

                    I like unions for this kind of stuff.

                    C++ is a perfectly valid school of magic.

                    Q 1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #13

                      here's an alternate solution.

                      std::vector<uint8_t> data = { 0x12U, 0x34U, 0x00U, 0x10U };
                      uint32_t v = 0U;
                      memcpy(&v, data.data(), sizeof(v));
                      
                      // to change endianness if necessary
                      uint8_t vPtr = &v;
                      for (size_t i = 0U; i < sizeof(v) / 2U; i++)) {
                          std::swap(*(vPtr + i), *(vPtr + 3U - i));
                      }
                      // v now contains a 32 bit uint comprised of the data from the vector
                      
                      1 Reply Last reply
                      2
                      • fcarneyF fcarney

                        Endianess? The little endian that could, or the big endian that interneted?

                        I like unions for this kind of stuff.

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #14

                        @fcarney can you give example for same using union

                        J.HilkJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @fcarney can you give example for same using union

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          @Qt-embedded-developer keep in mind, that unions are more generally speaking a C remnant.

                          For most cases C++ offers better and saver alternatives.
                          If you use a union for low level stuff like here in this example, you're most likely be fine, but if you start stuffing full fetched classes /structs into unions, you'll most likely run into lifetime/constructor/destructor issues and other UB's


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          2
                          • fcarneyF Offline
                            fcarneyF Offline
                            fcarney
                            wrote on last edited by
                            #16

                            @Qt-embedded-developer said in How to convert hex array data to integer ?:

                            can you give example for same using union

                                union {
                                    uint16_t u16;
                                    struct {
                                        uint8_t low;
                                        uint8_t high;
                                    } bytes;
                                    uint8_t bytearray[2];
                                } un1;
                            
                                un1.u16 = 0x4321;
                                qDebug() << Qt::hex << un1.bytes.low;
                                qDebug() << Qt::hex << un1.bytes.high;
                                qDebug() << Qt::hex << un1.bytearray[0];
                                qDebug() << Qt::hex << un1.bytearray[1];
                            

                            If you are doing a large set it is probably just as easy to do shifting.
                            You have to be very careful with data sizes here. That is why I used explicitly sized data types.

                            C++ is a perfectly valid school of magic.

                            1 Reply Last reply
                            0
                            • Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on last edited by
                              #17

                              you guys are missing an esoteric problem that is common in embedded systems. data alignmnent is not guaranteed. That's why brute force (while more complex) is more reliable. If using unions then at least make sure to pragma pack() your structs. There is also this c++ language spec annoyance that says the compiler is free to rearrange struct members to optimize memory usage. While I've never seen is happen in the real world, making assumptions about data ordering "could" bite you in the butt.

                              1 Reply Last reply
                              1

                              • Login

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