Unsolved Converting float datatype values as array of bytes
-
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,
-
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 amemcpy
to an array of unsigned char... -
i have edited my post and provided the output of int value.
Thanks,
-
@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.
-
i got 0,
Thanks,
-
@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.
-
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
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 amemcpy
, 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? -
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.
-
@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.htmlHowever, 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.