none
IEEE-754 Floating-Point Conversion From 32-bit Hexadecimal Representation To Decimal Floating-Point

    Question

  • hi,

      i am working in windows service which getting data from serial port. Data actually sent from hardware device using hexadecimal value and i got as byte array in serial port.

      Now they send the actual data as 32bit floating point hex value and i need to convert to decimal to get the actual value. Kindly help me to convert using c# language.

    eg: 000040a0 (This is sample data they sent as hex value) and the expected output is '5'. The conversion is taken from the following link

    http://babbage.cs.qc.cuny.edu/IEEE-754.old/32bit.html.

    Actually hardware team give the input as 40a00000 to find the output in the above link

    Kindly help to convert the same in c#


    • Edited by akhilrajau Sunday, June 02, 2013 10:29 AM
    Sunday, June 02, 2013 10:28 AM

Answers

  • Hi,

    See the BitConverter class :

                var b=BitConverter.GetBytes(0x40a00000);
               MessageBox.Show(BitConverter.ToSingle(b,0).ToString());
    


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by akhilrajau Sunday, June 02, 2013 3:58 PM
    Sunday, June 02, 2013 11:54 AM

All replies

  • Hi,

    See the BitConverter class :

                var b=BitConverter.GetBytes(0x40a00000);
               MessageBox.Show(BitConverter.ToSingle(b,0).ToString());
    


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by akhilrajau Sunday, June 02, 2013 3:58 PM
    Sunday, June 02, 2013 11:54 AM
  • thanks friends,

     hi stefan,

     I already read that topic but didn't get how i proceed. From Patrice example i got where i did the mistake.




    Sunday, June 02, 2013 4:01 PM
  • Try this code

           static void Main(string[] args)
            {
                string input = "40a00000";
                string output = Hex32toFloat(input);
                Console.WriteLine(output);
                input = "000040a0";
                output = Hex32toFloat(input);
                Console.WriteLine(output);
                Console.ReadLine();
            }
            static string Hex32toFloat(string Hex32Input)
            {
                string doublestr = "";
                UInt64 bigendian;
                bool success = UInt64.TryParse(Hex32Input,
                    System.Globalization.NumberStyles.HexNumber, null,   out bigendian);
                if (success)
                {
                    double fractionDivide = Math.Pow(2, 23);
                    double doubleout;
                    int sign = (bigendian & 0x80000000) == 0 ? 1 : -1;
                    Int64 exponent = ((Int64)(bigendian & 0x7F800000) >> 23) - (Int64)127;
                    UInt64 fraction = (bigendian & 0x007FFFFF);
                    if (fraction == 0)
                        doubleout = sign * Math.Pow(2, exponent);
                    else
                        doubleout = sign * (1 + (fraction / fractionDivide)) * Math.Pow(2, exponent);
                    doublestr = doubleout.ToString();
                }
                return doublestr;
            }


    jdweng

    Sunday, June 02, 2013 8:21 PM