tags:

views:

77

answers:

3

I have an array of three bytes, I want to convert array into double using c#. Kindly guide me.

+3  A: 

Well, that depends on what you want the conversion to do.

You can convert 8 bytes (in the right format) into a double using BitConverter.ToDouble - but with only three bytes it's a bit odd - after all, a double has 64 bits of information, normally. How do those three bytes represent a number? What's the format, basically? When you've figured that out, the rest may well be easy.

Jon Skeet
+2  A: 

Depends on what exactly is stored in the bytes, but you might be able to just pad the array with 5 bytes all containing 0 and then use BitConverter.ToDouble.

ho1
+1  A: 

Well a double is an array of 8 bytes, so with 3 bytes you won't have all the possible values.

To do what you want:

var myBytes[] = {0,0,0,0,0,1,1,2}; //assume you pad your array with enough zeros to make it 8 bytes.
var myDouble = BitConverter.ToDouble(myBytes,0);
Alan
@Alan, I have tried this approach, but it cause the exception. I don't know why.
Arman
Is your data in the correct floating point format (i believe that C# uses the standard IEEE Floating Point format)
Alan
OK I recheck my code.
Arman