简体   繁体   中英

How to convert any value to byte array and append it to a byte list?

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

You could use std::memcpy like this:

#include <vector>
#include <cstring>

template<typename T>
void serialize(std::vector<char>& v, const T& obj){
    auto size = v.size();
    v.resize(size+sizeof(T));

    std::memcpy(&v[size], &obj, sizeof(T));
}

Deserialization can be done similarly:

template<typename T>
void deserialize(const std::vector<char>& v, std::size_t offset, T& obj){      
     std::memcpy(&obj, &v[offset],sizeof(T));
}

You should get familiar with the strict aliasing rule before you do any type-casting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM