HowTo Convert Array to Binary

G

Guest

I have an array of an array of doubles and need to save this as a single byte
array.
Then save this byte array as a BLOB to an SQL 2005 database.

Array allocation code shown below:
Array myArray =
Array.CreateInstance(System.Type.GetType("System.Double[]"), 3);
Double[] dblArray1 = new double[4] { 23.56, 24.56, 25.56, 26.56 };
Double[] dblArray2 = new double[4] { 33.56, 34.56, 35.56, 36.56 };
Double[] dblArray3 = new double[4] { 43.56, 44.56, 45.56, 46.56 };
// Place above three double arrays in an myArray
myArray.SetValue(dblArray1, 0);
myArray.SetValue(dblArray2, 1);
myArray.SetValue(dblArray3, 2);

I have tried serialize the myArray object however the byte array is almost
twice the length of the array of doubles:
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, dblArray1);
BinaryReader reader = new BinaryReader(stream);
reader.BaseStream.Position = 0;
byte[] inputBytes = reader.ReadBytes((int)reader.BaseStream.Length);

However the byte count of the byte array is almost doubled that of the array
of doubles.
inputBytes byte count is 175 should of been 96 bytes.

Does anyone know a better way to save an Array of an Array of doubles to a
byte array?
Thanks in advance,
Scott
 
G

Guest

Correction - The following line for the formatter should of been this:
formatter.Serialize(stream, myArray);
 
M

Miha Markic [MVP C#]

Serializer stores additional metadata to be able to recover the structure.
There are several options, such as:
- use compression
- provide your own serialization mechanism
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top