开发者

.net converting bytearray to double[]

开发者 https://www.devze.com 2022-12-26 11:01 出处:网络
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

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


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.


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;

0

精彩评论

暂无评论...
验证码 换一张
取 消