tags:

views:

46

answers:

2

Hello,

I am working with a database from a legacy app which stores 24 floating point values (doubles) as a byte array of length 192, so 8 bytes per value. This byte array is stored in a column of type image in a SQL Server 2005 database.

In my .net app I need to read this byte array and convert it to a array of type Double[24]. I can access the field easy enough reader.GetBytes(...) but how to convert the returned ByteArray to Double[24]

Any ideas?

Thanks,

AJ

+3  A: 

Well, how is each set of 8 bytes represented? You may be able to use Buffer.BlockCopy:

double[] doubles = new double[bytes.Length / 8];
Buffer.BlockCopy(bytes, 0, doubles, 0, bytes.Length);

or you may need to use BitConverter.ToDouble repeatedly - or some custom conversion method.

Jon Skeet
Thanks, each 8 bytes is a standard IEEE 754-1985 double.
AJ
@AJ: In what endianness? I *believe* doubles have endianness internally, which makes comparisons really straightforward.
Jon Skeet
@Jon Skeet: I'm not sure, an example of the first 8 bytes (in hex) is: 0000000000002840
AJ
It appears the documentation for BitConverter.ToDouble shows that BitConverter assumes the Byte array is little endian, whereas the hex string you've got is big endian.
Ankur Goel
A: 

brute force:

double[] doubles = new double[24];   

for (int i=0; i < 24; i++)
{
    for (int j=0; j < 8; j++)
    {
        doubles[i] += bytes[i*8 + j] << j*8;
    }
}

if the endianness is wrong, change the last term from j*8 to (7-j)*8;

Ankur Goel