I want to write a custom binary file for my little game engine. I worked with C# and I know how to do it in C# with BitConvert or Convert.
But for some reasons I need to do the same in C++.
Here's my data struct :
Mesh Position point3(x,y,z) type: float size: 4bytes *3 = 12bytes
Mesh Options point3(b1,b2,b3) type: bool size: 1byte *3 = 3bytes
Mesh Scale point3(x,y,z) type: int size: 4bytes *3 = 12bytes
Mesh Tag single type: string size: 8bytes *1 = 8bytes
Now I have an list of unsigned chars :
vector<unsigned char> binary_data;
And I can use push to add single bytes into it and finally write it with ofstream :
ofstream fout;
fout.open("file.bin", ios::binary | ios::out);
vector<unsigned char> binary_data;
binary_data.push_back(0x66);
binary_data.push_back(0xFF); // bool : true
binary_data.push_back(0x00); // bool : false
fout.write((char*)binary_data.data(),binary_data.size());
fout.close();
My Problem
I need to convert my values of any type float,int,bool,uint,double to byte array and append it to my vector list.
And again get them and convert them back to original value.
offsets and byte sizes are all known.
Here's an example : There's two meshes in scene ..
- Scale of first : point3(10,5,15)
- Scale of second : point3(25,0,5)
Now I get x y z and convert them to 4 bytes :
10 = > 00 00 00 0A
5 = > 00 00 00 05
15 = > 00 00 00 0F
25 = > 00 00 00 19
0 = > 00 00 00 00
5 = > 00 00 00 05
And I append them to binary_data
list.
How can I convert them to byte array and back ?