3

I have tried the below C# code to convert from hex literal to floating point and get the correct result. I wish to input a byte array instead and have that converted to floating point but can't seem to get it right result.

0x4229ec00 is the current format. I need it in byte array format something like...

new byte[]{ 0x01, 0x04, 0x01, 0x60, 0x00, 0x02, 0x70, 0x29}; //current output 42.48

The code looks like:

byte[] bytes = BitConverter.GetBytes(0x4229ec00);
float myFloat = floatConversion(bytes);

public float floatConversion(byte[] bytes)
{
    float myFloat = BitConverter.ToSingle(bytes, 0);
    return myFloat;
}

Any help would be greatly appreciated. Thank you!

5
  • @JamesBarrass: He's converting 4 bytes, not 8: #42, #29, #ec and #00. Commented Jan 30, 2014 at 10:47
  • @RoyDictus: "something like... 0x01, 0x04, 0x01, 0x60, 0x00, 0x02, 0x70, 0x29 Commented Jan 30, 2014 at 10:50
  • @Maria88: the size of float is indeed 4 bytes, not 8, so if you are converting that 8-byte array to a floating point, it won't be a float but a double. Commented Jan 30, 2014 at 10:50
  • Please describe the problem clearly. You want to convert an array of 4N bytes into N floats. Is that it? What part are you stuck on? Commented Jan 30, 2014 at 10:55
  • What we are struggling with is what you want to do with 8 bytes. Convert them into two floats, or one double. And some clarity on the byte ordering would help. Why are your bytes in that order? Do they arrive down the wire? Commented Jan 30, 2014 at 11:12

2 Answers 2

7

You can amend your float conversion function as below

    public float floatConversion(byte[] bytes)
    {
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(bytes); // Convert big endian to little endian
        }
        float myFloat = BitConverter.ToSingle(bytes, 0);
        return myFloat;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

float (Single) is a 4 Byte value;

Your test value 0x4229ec00 contains 4 bytes, they are: 0x42, 0x29, 0xEC, 0x00

x86 CPUs use reversed order of bytes (Little Endian), so the right byte array is

0x00, 0xEC, 0x29, 0x42

The Code

// Original array
Byte[] data = new Byte[] {0x42, 0x29, 0xEC, 0x00};
// 42.48047
// If CPU uses Little Endian, we should reverse the data 
float result = BitConverter.ToSingle(BitConverter.IsLittleEndian? data.Reverse().ToArray() : data, 0);

4 Comments

@Maria88 We are all looking to help. We just need some more info. The question you asked is not clear.
Very Sorry if i offended you,was just looking for some help,ill be more clear next time ,Very Sorry,Can anyone help with the reversal of the bytes in the byte array ,is there a simple one line conversion?
@Maria88: Yes, you can do it in one line, see my edit
Thats been a great contribution thank you very much,i continue to have the best respect for this website and will try to contribute also when i can ,its because of people like you that software engineering has come so far,thank you

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.