Serialization:
bool[] tempArray = new bool[MapWidth * MapHeight];
for (int i = 0; i < MapWidth; i++)
{
for (int ii = 0; ii < MapHeight; ii++)
{
tempArray[i * ii] = mapSolid[i, ii];
}
}
BitArray bits = new BitArray(tempArray);
byte[] bytes = new byte[32 * MapWidth];
bits.CopyTo(bytes, 0);
bin.Serialize(stream, Convert.ToBase64String(bytes));
Desrialization:
byte[] bytes = new byte[255 * 255];
bytes=Convert.FromBase64String((string)bin.Deserialize(stream));
//mapSolid = ;
MapWidth = (int)bin.Deserialize(stream);
MapHeight = (int)bin.Deserialize(stream);
BitArray bits = new BitArray(bytes);
bool[] bytestemp = new bool[255*265];
bits.CopyTo(bytestemp, 0);
for (int i = 0; i < MapWidth; i++)
{
for (int ii = 0; ii < MapHeight; ii++)
{
mapSolid[i, ii] = bytestemp[i*ii];
}
}
Now everything seems to work without errors but the map looses it’s structure after deserialization.
and
is wrong; that index should be something like:
or
depending on whether you are writing rows-first or columns-first.
Otherwise, for example, the cells {1,4} and {4,1} would be stored in the same place.
If you want to avoid the math, another common approach is just:
and
i.e. just read/write from/to “the next position” each time. Just make sure the read and write loop agree on which is the outer/inner loop (they seem to).