HowTo Convert Array of Array to BLOB

  • Thread starter Thread starter Guest
  • Start date Start date
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, myArray);
BinaryReader reader = new BinaryReader(stream);
reader.BaseStream.Position = 0;
byte[] inputBytes = reader.ReadBytes((int)reader.BaseStream.Length);

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 without this additional bytes?

Thanks in advance,
Scott
 
Scott said:
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, myArray);
BinaryReader reader = new BinaryReader(stream);
reader.BaseStream.Position = 0;
byte[] inputBytes = reader.ReadBytes((int)reader.BaseStream.Length);

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 without this additional bytes?

Use BitConverter to convert each double to bytes. You could use my
EndianBitConverter which, as well as letting you specify the
endianness, lets you copy into an existing byte array instead of always
returning a new one.

Note that the serialization above is saving more information than can
be stored in 96 bytes though - it's saving the fact that those doubles
are in 3 arrays, each of which is a double[4]. If you need that
information to be saved as well, you'll have to either work out your
own way of doing it or go with the serialization.
 
Back
Top