Search code examples
c#floating-pointmodbusbitconverterushort

Converting To Floating Point


I have two ushort integer. Integers : 2206,41247

I want to convert to float this integers. For this;

void Main()
{
    GetSingle(2206,41247).Dump();
}
public static float GetSingle(ushort highOrderValue, ushort lowOrderValue)
{
    return BitConverter.ToSingle(BitConverter.GetBytes(lowOrderValue).Concat(BitConverter.GetBytes(highOrderValue)).ToArray(), 0);
}

I coded something in Linqpad. This functions result is 9,547158E-34

But in Modbus Poll program converts 0 this integers.

I reading this integers from remote device with Modbus Protocol. And correct value is 0

My question; How the Modbus Poll program converts this integers to float ? How can value be zero ?

Any idea ?


Solution

  • I arrived at the same number. Remember that 9,547158E-34 is really tiny - maybe it is just cast to an int afterwards, yeilding 0?