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. 4 byte uint8_t array to integer conversion.
QtWS25 Last Chance

4 byte uint8_t array to integer conversion.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.3k 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.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by
    #1

    Hello, I am trying to make a TCP connection with some client. Client keep sending me an integer value in an uint8_t array. Since it is an integer value it is 4 bytes. I am trying to use this values (printing, putting into table etc.) However, when I try to convert this 4 byte uint8_t array into integer. That becomes a completely different data.
    I try this line in order to convert that array into integer and then to string:

    convertedString= QString::number(*(int*)receivedFrame.data);
    

    receivedFrame is a structure:

    typedef struct{
    
        //! Variable which holds the header data of the  Frame
        HEADER headerData;
    
        //! Data contained in the variable size portion of the
        uint8_t data[MAX_DATA_LENGTH];
    }FRAME;
    
    How can convert this uint8_t data array into an integer? Thanks beforehand.
    J.HilkJ 1 Reply Last reply
    0
    • G GunkutA

      Hello, I am trying to make a TCP connection with some client. Client keep sending me an integer value in an uint8_t array. Since it is an integer value it is 4 bytes. I am trying to use this values (printing, putting into table etc.) However, when I try to convert this 4 byte uint8_t array into integer. That becomes a completely different data.
      I try this line in order to convert that array into integer and then to string:

      convertedString= QString::number(*(int*)receivedFrame.data);
      

      receivedFrame is a structure:

      typedef struct{
      
          //! Variable which holds the header data of the  Frame
          HEADER headerData;
      
          //! Data contained in the variable size portion of the
          uint8_t data[MAX_DATA_LENGTH];
      }FRAME;
      
      How can convert this uint8_t data array into an integer? Thanks beforehand.
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #3

      @GunkutA
      you can also try something like this,

      uint32_t i32 = receivedFrame.data[0] | (receivedFrame.data[1] << 8) | (receivedFrame.data[2] << 16) | (receivedFrame.data[3] << 24);

      Simply change around the indices for a correction of the byte order


      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
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @GunkutA said in 4 byte uint8_t array to integer conversion.:

        convertedString= QString::number((int)receivedFrame.data);

        an ampersand is missing, using c++ functionality would have given a compiler error

        int value = reinterpret_cast<int>(&receivedFrame.data);

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        G 1 Reply Last reply
        4
        • G GunkutA

          Hello, I am trying to make a TCP connection with some client. Client keep sending me an integer value in an uint8_t array. Since it is an integer value it is 4 bytes. I am trying to use this values (printing, putting into table etc.) However, when I try to convert this 4 byte uint8_t array into integer. That becomes a completely different data.
          I try this line in order to convert that array into integer and then to string:

          convertedString= QString::number(*(int*)receivedFrame.data);
          

          receivedFrame is a structure:

          typedef struct{
          
              //! Variable which holds the header data of the  Frame
              HEADER headerData;
          
              //! Data contained in the variable size portion of the
              uint8_t data[MAX_DATA_LENGTH];
          }FRAME;
          
          How can convert this uint8_t data array into an integer? Thanks beforehand.
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @GunkutA
          you can also try something like this,

          uint32_t i32 = receivedFrame.data[0] | (receivedFrame.data[1] << 8) | (receivedFrame.data[2] << 16) | (receivedFrame.data[3] << 24);

          Simply change around the indices for a correction of the byte order


          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
          • Christian EhrlicherC Christian Ehrlicher

            @GunkutA said in 4 byte uint8_t array to integer conversion.:

            convertedString= QString::number((int)receivedFrame.data);

            an ampersand is missing, using c++ functionality would have given a compiler error

            int value = reinterpret_cast<int>(&receivedFrame.data);

            G Offline
            G Offline
            GunkutA
            wrote on last edited by
            #4

            @Christian-Ehrlicher That gives me "cast from pointer to smaller type 'int' loses information" error. I think I forgot to add the complete function which is:

            QString protocolForm::convertToString(uint8_t *data,int dataLength)
            {
                FRAME receivedFrame;
                memcpy(&receivedFrame,data,dataLength);
            memcpy(&receivedHeader, receivedFrame.data, sizeof(PARAM_DATA_HEADER));
            convertedString= QString::number(*(int*)&receivedFrame.data);
            }
            

            So data is a pointer

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              This is not an error, just a warning which is understandable since you do some crude casting (but in this case it's wanted)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GunkutA
                wrote on last edited by
                #6

                Sorry I tried to use it in switch statement. Then it gave

                jump tp case label [-fpermissive] 
                note: crosses initialization of'int integer' 
                

                Error with

                cast from 'uint8_t (*) [255] {aka unsigned char (*) [255]} 'to 'int' loses precision [-fpermissive] 
                

                error.

                G 1 Reply Last reply
                0
                • G GunkutA

                  Sorry I tried to use it in switch statement. Then it gave

                  jump tp case label [-fpermissive] 
                  note: crosses initialization of'int integer' 
                  

                  Error with

                  cast from 'uint8_t (*) [255] {aka unsigned char (*) [255]} 'to 'int' loses precision [-fpermissive] 
                  

                  error.

                  G Offline
                  G Offline
                  GunkutA
                  wrote on last edited by
                  #7

                  @GunkutA said in 4 byte uint8_t array to integer conversion.:

                  jump tp case label [-fpermissive]
                  note: crosses initialization of

                  When I added { } to the case I got rid of the first error. But

                  cast from 'uint8_t (*) [255] {aka unsigned char (*) [255]} 'to 'int' loses precision [-fpermissive] 
                  
                  

                  error still remains.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #8

                    @GunkutA said in 4 byte uint8_t array to integer conversion.:

                    error still remains.

                    This is no error, it's a warning that the cast is not really a good idea and int should be used in the first place in your struct. You can get rid of it by using @J-Hilk 's suggestion

                    The forum displays it wrong:

                    int value = *reinterpret_cast<int *>(&receivedFrame.data);

                    Now it's fine, must be a space between 'int' and '' - otherwise the forum eats the '' - sorry.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2

                    • Login

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