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 9.0k 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.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on 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
    • Pradeep KumarP Offline
      Pradeep KumarP Offline
      Pradeep Kumar
      wrote on last edited by Pradeep Kumar
      #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

      JKSHJ 1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by JohanSolo
        #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
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on 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
          0
          • Pradeep KumarP Pradeep Kumar

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

            Thanks,

            JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by JohanSolo
            #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
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #8

              i got 0,

              Thanks,

              Pradeep Kumar
              Qt,QML Developer

              JohanSoloJ 1 Reply Last reply
              0
              • Pradeep KumarP Pradeep Kumar

                i got 0,

                Thanks,

                JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on 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
                • Pradeep KumarP Offline
                  Pradeep KumarP Offline
                  Pradeep Kumar
                  wrote on 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.sueM 2 Replies Last reply
                  0
                  • Pradeep KumarP Pradeep Kumar

                    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 last edited by JohanSolo
                    #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
                    • Pradeep KumarP Pradeep Kumar

                      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.sueM Offline
                      m.sueM Offline
                      m.sue
                      wrote on 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
                      • Pradeep KumarP Pradeep Kumar

                        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,

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 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

                        • Login

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