Converting float datatype values as array of bytes
-
wrote on 3 Aug 2017, 10:46 last edited by
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,
-
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,
wrote on 3 Aug 2017, 15:26 last edited byHi @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-functionatof
.-Michael.
-
First, remember that on a modern PC:
char
is 1 bytefloat
is 4 bytesdouble
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?
-
wrote on 4 Aug 2017, 08:35 last edited by Pradeep Kumar 8 Apr 2017, 09:53
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,
-
wrote on 4 Aug 2017, 09:38 last edited by JohanSolo 8 Apr 2017, 09:45
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... -
wrote on 4 Aug 2017, 09:53 last edited by
i have edited my post and provided the output of int value.
Thanks,
-
i have edited my post and provided the output of int value.
Thanks,
wrote on 4 Aug 2017, 09:56 last edited by JohanSolo 8 Apr 2017, 10:03@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.
-
wrote on 4 Aug 2017, 10:04 last edited by
i got 0,
Thanks,
-
i got 0,
Thanks,
wrote on 4 Aug 2017, 10:06 last edited by@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.
-
wrote on 4 Aug 2017, 10:09 last edited by
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,
-
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,
wrote on 4 Aug 2017, 10:16 last edited by JohanSolo 8 Apr 2017, 10:19@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? -
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,
wrote on 4 Aug 2017, 12:18 last edited byYou 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.
-
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 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.
1/13