Conversion of C# String into float value

In this article we will discuss about how to convert the C# String into floating point value.
  • 4439

In this article we are discussing about how to convert the string value into float value. For this we will use System.BitConverter Class which contains so many methods one of which is GetBytes() which returns the specified integer value as an array of bytes and other is ToSingle() method returns a single-precision floating point number converted at a specified position in a byte array.

Lets have a look on the following example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication15

{

    class Program

    {

        static void Main(string[] args)

        {

            string hexastr = "53580150";

            uint number = uint.Parse(hexastr, System.Globalization.NumberStyles.AllowHexSpecifier);

 

            byte[] floatvalue = BitConverter.GetBytes(number);

            float fn = BitConverter.ToSingle(floatvalue, 0);

            Console.WriteLine("float convert = {0}", fn);

            Console.ReadLine();

        }

    }

}

Output:

 Image24.jpg

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.