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

RE: HowTo Convert Array to Binary by Scott

Scott
Sat Jan 20 18:56:00 CST 2007

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

Re: HowTo Convert Array to Binary by Miha

Miha
Sun Jan 21 07:39:43 CST 2007

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

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Scott" <Scott@discussions.microsoft.com> wrote in message
news:D0FE3D0E-133D-448C-9008-2C517B6591AB@microsoft.com...
>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