I want to store an sbyte[,] on my disk using as little space as possible (cant take more than a few seconds to save or load though) and get it back at a later time.
I can't serialize it to xml: Cannot serialize object of type System.SByte[,]. Multidimensional arrays are not supported.
And I can't convert it to a MemoryStream: Cannot convert from 'sbyte[,]' to 'int'
Besides creating a text file and looping it out piece by piece.. what options are there.
If it makes any difference the array can be upwards of 100 000 x 100 000 in size. The file also needs to be usable by different operating systems and computers.
Update.
I went with flattening my array down to a 1D sbyte[]
and then converted the sbyte[]
to a stream and saving it to disk along with a separate file containing the dimensions.
Stream stream = new MemoryStream(byteArray);
Used this as a base for saving the stream to disk. https://stackoverflow.com/a/5515894/937131
This is a testcase I wrote for the flattening and unflattening if anyone else finds it usefull.
[TestMethod]
public void sbyteTo1dThenBack()
{
sbyte[,] start = new sbyte[,]
{
{1, 2},
{3, 4},
{5, 6},
{7, 8},
{9, 10}
};
sbyte[] flattened = new sbyte[start.Length];
System.Buffer.BlockCopy(start, 0, flattened, 0, start.Length * sizeof(sbyte));
sbyte[,] andBackAgain = new sbyte[5, 2];
Buffer.BlockCopy(flattened, 0, andBackAgain, 0, flattened.Length * sizeof(sbyte));
var equal =
start.Rank == andBackAgain.Rank &&
Enumerable.Range(0, start.Rank).All(dimension => start.GetLength(dimension) == andBackAgain.GetLength(dimension)) &&
andBackAgain.Cast<sbyte>().SequenceEqual(andBackAgain.Cast<sbyte>());
Assert.IsTrue(equal);
}