5

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

2
  • 2
    Providing your code would be a good start to getting an answer Commented Aug 6, 2010 at 7:13
  • What endian notation are you using? Commented Aug 6, 2010 at 7:56

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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);

2 Comments

I have tried this approach, but it cause the exception. I don't know why.
Is your data in the correct floating point format (i believe that C# uses the standard IEEE Floating Point format)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.