I am stuck at typical problem of conversion of data formats in WPF application in C#. I am new to C#. The GUI i am working on is displaying temperatures after getting the bytes data from the UDS messages. When the data received in the format like 41c00000( which is in float format), the below function is able to convert the data into temperature in Celsius (both negative and positive).
public static float ComposeFloat(List<string> list, bool bigEndian)
{
float val;
byte[] payload = { 0, 0, 0, 0 }; // Used for float conversions
// BitConverter needs a little endian list
if (bigEndian)
{
list.Reverse();
}
for (int i = 0; i < 4; i++)
{
payload[i] = Convert.ToByte(list[i], 16);
}
val = BitConverter.ToSingle(payload, 0); // convert to an ieee754 float
return val;
}
But for some other temperature sensor in our system, the UDS is giving the data in the format 00000028. Since the UDS message was giving the temperature in integer format, I modified the above code as shown below, completely ignoring the case about what will happen if the temperature would be negative, which is really big mistake.
public static float ComposeFloat_FromInt(List<string> list, bool bigEndian)
{
float val;
int[] payload = { 0, 0, 0, 0 };
if (bigEndian)
{
list.Reverse();
}
for (int i = 0; i < 4; i++)
{
payload[i] = Convert.ToInt32(list[i], 16);
}
val = (float)payload[0];
return val;
}
Please guide me what will be the data received from the system when temperature is negative by giving some example and how should i modify the function to cover the negative temperature case.