46

I have a byte array of size 4

byte[] source = new byte[4];

Now I wanted to convert this source into a 4-byte float value...

Can anyone tell me how to do this...

1
  • 2
    In what numeric format is the byte array? There are many different ways it might be interpreted. Do you have an example? For instance, the bytes of a 32-bit integer, least significant byte at byte 0, each byte little-endian. Commented Apr 12, 2010 at 3:03

1 Answer 1

75

Try

float myFloat = System.BitConverter.ToSingle(mybyteArray, startIndex);

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

2 Comments

Suppose the bytearray came over a network. Is Endianness a concern here? If I wanted to write the sequence of bytes in C or C++ what should I do?
@mazzaa Yes it is a concern: in case System.BitConverter.IsLittleEndian does not agree the network source, you have to swap the 4 array bytes first

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.