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. Converting float datatype values as array of bytes
Forum Updated to NodeBB v4.3 + New Features

Converting float datatype values as array of bytes

Scheduled Pinned Locked Moved Unsolved C++ Gurus
13 Posts 4 Posters 8.9k Views 3 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.
  • P Offline
    P Offline
    Pradeep Kumar
    wrote on 3 Aug 2017, 10:46 last edited by
    #1

    Hi,

    I am making integer dataype and string datatype in the form of array of bytes and back from array of bytes to respective datatype (integer and string).
    I understood integer and string values. i also able to implement
    doing serialization and deserialization.

    I am trying with float, i am nt getting and googling also, didnt undertood.
    can anyone help me with this.

    Ex i tried:
    was :

    #define round(x) (int)floor( x + 0.5f)
    
    int convertDoubleToInt(float val);
    float convertIntToDouble(int val);
    
    int main(int argc, char *argv[])
    {
        unsigned char buffer[10];
        float value = 65.3;
    
        buffer[0] = (convertDoubleToInt(value) & 0xFF);
        buffer[1] = (convertDoubleToInt(value) >> 8) & 0xFF;
        cout << "float value :" <<  buffer[0] << buffer[1]  ;
    
        int original_value = buffer[0];
        float g =   convertIntToDouble(original_value);
        cout << g;
    
    
        return 0;
    }
    
    int convertDoubleToInt(float val)
    {
        return round(val * 10);
    }
    
    float convertIntToDouble(int val)
    {
        float temp = val * 0.1;
        return temp;
    }
    

    And can u help with the memory representation with the above example?.

    Thanks,

    Pradeep Kumar
    Qt,QML Developer

    M 1 Reply Last reply 3 Aug 2017, 15:26
    0
    • P Pradeep Kumar
      3 Aug 2017, 10:46

      Hi,

      I am making integer dataype and string datatype in the form of array of bytes and back from array of bytes to respective datatype (integer and string).
      I understood integer and string values. i also able to implement
      doing serialization and deserialization.

      I am trying with float, i am nt getting and googling also, didnt undertood.
      can anyone help me with this.

      Ex i tried:
      was :

      #define round(x) (int)floor( x + 0.5f)
      
      int convertDoubleToInt(float val);
      float convertIntToDouble(int val);
      
      int main(int argc, char *argv[])
      {
          unsigned char buffer[10];
          float value = 65.3;
      
          buffer[0] = (convertDoubleToInt(value) & 0xFF);
          buffer[1] = (convertDoubleToInt(value) >> 8) & 0xFF;
          cout << "float value :" <<  buffer[0] << buffer[1]  ;
      
          int original_value = buffer[0];
          float g =   convertIntToDouble(original_value);
          cout << g;
      
      
          return 0;
      }
      
      int convertDoubleToInt(float val)
      {
          return round(val * 10);
      }
      
      float convertIntToDouble(int val)
      {
          float temp = val * 0.1;
          return temp;
      }
      

      And can u help with the memory representation with the above example?.

      Thanks,

      M Offline
      M Offline
      m.sue
      wrote on 3 Aug 2017, 15:26 last edited by
      #2

      Hi @Pradeep-Kumar,

      I am not sure what you want as result. Just converting a double into a string, you can do with the C-function sprintf. The other way round with the C-function atof.

      -Michael.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 4 Aug 2017, 08:13 last edited by
        #3

        First, remember that on a modern PC:

        • char is 1 byte
        • float is 4 bytes
        • double is 8 bytes

        Your code is trying to put a 4-byte value into a 2-byte buffer. That won't fit.

        Second, can you please explain why you want to convert float/double into a byte array?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • P Offline
          P Offline
          Pradeep Kumar
          wrote on 4 Aug 2017, 08:35 last edited by Pradeep Kumar 8 Apr 2017, 09:53
          #4

          Hello,

          had to do serialize of datatype values, i got for int, string,char and enum.
          was doing it for float. so i posted the question,

          K can u explain how can i serialize the float value and then back deserialize to get the original float value.

          As i did for int :

          int Value = 600;
          
           unsigned char buffer[10];
          
          buffer[0]  = Value & 0xFF;
          
            int secondByte = Value  >> 8;
            buffer[1] = secondByte & 0xFF;
          
            cout <<  "serialize :" <<  " value of Array[0] 600 : " << buffer[0] << endl;
            cout <<  "serialize :"  << " value of Array[1] 600 : " << buffer[1]   << endl;
          
            cout  <<  "deserialize :" << ((buffer[1] << 8) | buffer[0]);
          

          output :

          cout << "serialize :" << " value of Array[0] 600 : " << 88 ;
          cout << "serialize :" << " value of Array[0] 600 : " << 2;

          cout << "deserialize :" << 600;

          How can i do for float values?.

          Thanks,

          Pradeep Kumar
          Qt,QML Developer

          J 1 Reply Last reply 4 Aug 2017, 13:08
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on 4 Aug 2017, 09:38 last edited by JohanSolo 8 Apr 2017, 09:45
            #5

            Your example for int does not work: int is at least 4 bytes on any modern PC IIRC. Therefore as @JKSH told you, you cannot do it.
            Then, could you please define what you mean with "serialise"? I guess you are simply reimplementing a memcpy to an array of unsigned char...

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Pradeep Kumar
              wrote on 4 Aug 2017, 09:53 last edited by
              #6

              i have edited my post and provided the output of int value.

              Thanks,

              Pradeep Kumar
              Qt,QML Developer

              JohanSoloJ 1 Reply Last reply 4 Aug 2017, 09:56
              0
              • P Pradeep Kumar
                4 Aug 2017, 09:53

                i have edited my post and provided the output of int value.

                Thanks,

                JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on 4 Aug 2017, 09:56 last edited by JohanSolo 8 Apr 2017, 10:03
                #7

                @Pradeep-Kumar said in Converting float datatype values as array of bytes:

                i have edited my post and provided the output of int value.

                Please try with int Value = 0x10000 and see what comes out.

                Edit: about type size, please have a look at the "Fundamental data types" section of the C++ documentation.

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Pradeep Kumar
                  wrote on 4 Aug 2017, 10:04 last edited by
                  #8

                  i got 0,

                  Thanks,

                  Pradeep Kumar
                  Qt,QML Developer

                  JohanSoloJ 1 Reply Last reply 4 Aug 2017, 10:06
                  0
                  • P Pradeep Kumar
                    4 Aug 2017, 10:04

                    i got 0,

                    Thanks,

                    JohanSoloJ Offline
                    JohanSoloJ Offline
                    JohanSolo
                    wrote on 4 Aug 2017, 10:06 last edited by
                    #9

                    @Pradeep-Kumar said in Converting float datatype values as array of bytes:

                    i got 0,

                    Exactly!

                    Therefore , please define what you mean with "serialise" your int / float / string.

                    `They did not know it was impossible, so they did it.'
                    -- Mark Twain

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Pradeep Kumar
                      wrote on 4 Aug 2017, 10:09 last edited by
                      #10

                      i have string, int , float values in a structure. so serialize all the values into array of bytes will provide serialize value,
                      then to get back the original values of respective datatype , deserialize will be required.
                      tats wat i am doing.

                      Ex:
                      int a;
                      string b;
                      float f;

                      storing in unsigned char buffer[400];

                      buffer[0] = int value;
                      buffer[1] = float value;
                      buffer[2] = string value;

                      then deserialize:

                      int a = buffer[0];
                      float f = buffer[2];
                      string b = buffer[1];

                      i hope i explained clearly or anything i missed?.

                      Thanks,

                      Pradeep Kumar
                      Qt,QML Developer

                      JohanSoloJ M 2 Replies Last reply 4 Aug 2017, 10:16
                      0
                      • P Pradeep Kumar
                        4 Aug 2017, 10:09

                        i have string, int , float values in a structure. so serialize all the values into array of bytes will provide serialize value,
                        then to get back the original values of respective datatype , deserialize will be required.
                        tats wat i am doing.

                        Ex:
                        int a;
                        string b;
                        float f;

                        storing in unsigned char buffer[400];

                        buffer[0] = int value;
                        buffer[1] = float value;
                        buffer[2] = string value;

                        then deserialize:

                        int a = buffer[0];
                        float f = buffer[2];
                        string b = buffer[1];

                        i hope i explained clearly or anything i missed?.

                        Thanks,

                        JohanSoloJ Offline
                        JohanSoloJ Offline
                        JohanSolo
                        wrote on 4 Aug 2017, 10:16 last edited by JohanSolo 8 Apr 2017, 10:19
                        #11

                        @Pradeep-Kumar
                        OK, then first you have to know the size of each serialised data (see @JKSH post and mine above).
                        Then, I'd simply go with a memcpy, here below's an example for uint32 and float:

                        float valueFloat( 3.14f );
                        quint32 valueUint32( 42u );
                        
                        char buffer[ 4u ];
                        
                        memcpy( buffer, &valueFloat, 4u );
                        // now buffer contains what you call "serialised" data
                        float readBackFloat( 0.f );
                        memcpy( &readBackFloat, buffer, 4u );
                        
                        memcpy( buffer, &valueUint32, 4u );
                        // now buffer contains what you call "serialised" data
                        quint32 readBackUint32( 0u );
                        memcpy( &readBackFloat, buffer, 4u );
                        

                        But you have to know the size of what you want to "serialise", to allocate the correct size for the buffer (a string can be as you as you want), and then you must know what contains the buffer to be able to perform the conversion from buffer to type.
                        Isn't QVariant what you are looking for?

                        `They did not know it was impossible, so they did it.'
                        -- Mark Twain

                        1 Reply Last reply
                        0
                        • P Pradeep Kumar
                          4 Aug 2017, 10:09

                          i have string, int , float values in a structure. so serialize all the values into array of bytes will provide serialize value,
                          then to get back the original values of respective datatype , deserialize will be required.
                          tats wat i am doing.

                          Ex:
                          int a;
                          string b;
                          float f;

                          storing in unsigned char buffer[400];

                          buffer[0] = int value;
                          buffer[1] = float value;
                          buffer[2] = string value;

                          then deserialize:

                          int a = buffer[0];
                          float f = buffer[2];
                          string b = buffer[1];

                          i hope i explained clearly or anything i missed?.

                          Thanks,

                          M Offline
                          M Offline
                          m.sue
                          wrote on 4 Aug 2017, 12:18 last edited by
                          #12

                          Hi @Pradeep-Kumar

                          You seem to think that with char buffer[400]; buffer[0], buffer [1], buffer[2] each have 400 bytes, but they are just the first, second and third byte of a 400 byte long string. So when you use buffer0, 1, 2 one serialization overwrites the next.

                          -Michael.

                          1 Reply Last reply
                          1
                          • P Pradeep Kumar
                            4 Aug 2017, 08:35

                            Hello,

                            had to do serialize of datatype values, i got for int, string,char and enum.
                            was doing it for float. so i posted the question,

                            K can u explain how can i serialize the float value and then back deserialize to get the original float value.

                            As i did for int :

                            int Value = 600;
                            
                             unsigned char buffer[10];
                            
                            buffer[0]  = Value & 0xFF;
                            
                              int secondByte = Value  >> 8;
                              buffer[1] = secondByte & 0xFF;
                            
                              cout <<  "serialize :" <<  " value of Array[0] 600 : " << buffer[0] << endl;
                              cout <<  "serialize :"  << " value of Array[1] 600 : " << buffer[1]   << endl;
                            
                              cout  <<  "deserialize :" << ((buffer[1] << 8) | buffer[0]);
                            

                            output :

                            cout << "serialize :" << " value of Array[0] 600 : " << 88 ;
                            cout << "serialize :" << " value of Array[0] 600 : " << 2;

                            cout << "deserialize :" << 600;

                            How can i do for float values?.

                            Thanks,

                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 4 Aug 2017, 13:08 last edited by
                            #13

                            @Pradeep-Kumar said in Converting float datatype values as array of bytes:

                            had to do serialize of datatype values

                            What is the purpose of your serialized data?

                            If you want to store your serialized data in a file, and then use your same application to read the stored data, then I recommend using QDataStream to serialize/deserialize it. You can find examples at http://doc.qt.io/qt-5/qdatastream.html

                            However, if you want to send your serialized data to a 3rd-party application, then you need to read the other application's documentation. There are countless different ways to serialize data.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            1

                            1/13

                            3 Aug 2017, 10:46

                            • Login

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