views:

65

answers:

2

When I do the following:

DECLARE @x float
SET @x = 1234.5
SELECT CONVERT(varbinary, @x)

What is the returned format? I need to get the original number back from .net somehow, and I only have the binary value available.

Clarification:

The statement above returns

(no column name)
0x40934A0000000000

Somehow, I must get the value 12345.5 back from the binary data 0x40934A0000000000.

A: 

The corresponding data type in C# to SQL Server varbinary is byte[]. But, why are you converting a float to a binary if you want to access it as a number?

RedFilter
I know the data type, but I need to know the format of the actual content. The reason for me doing this is that I have a bunch of values of different types in the same (view) column, and some of them are UDTs so I can't use sql_variant.
erikkallen
A: 

Apparently the format is IEEE 64-bit, but the byte ordering is the reverse of what BitConverter.ToDouble expects. The number can be retrieved with this code:

long bits = 0;
for (int i = 0; i < 8; i++)
    bits = (bits << 8) | data[i];
return BitConverter.Int64BitsToDouble(bits);
erikkallen