# Converting float datatype values as array of bytes

• 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,

• 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.

• 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?

• 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 a `memcpy` to an array of unsigned char...

• 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.

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,

• 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,

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

memcpy( buffer, &valueUint32, 4u );
// now buffer contains what you call "serialised" data
``````

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.

• 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.