BitConverter.ToSingle Method

Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SecuritySafeCriticalAttribute]
public static float ToSingle(
	byte[] value,
	int startIndex
)

Parameters

value
Type: System.Byte[]
An array of bytes.
startIndex
Type: System.Int32
The starting position within value.

Return Value

Type: System.Single
A single-precision floating point number formed by four bytes beginning at startIndex.

ExceptionCondition
ArgumentException

startIndex is greater than or equal to the length of value minus 3, and is less than or equal to the length of value minus 1.

ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of value minus 1.

The following code example converts elements of Byte arrays to Single values with the ToSingle method.


// Example of the BitConverter.ToSingle method.
using System;

class Example
{
   const string formatter = "{0,5}{1,17}{2,18:E7}";

   // Convert four byte array elements to a float and display it.
   public static void BAToSingle(System.Windows.Controls.TextBlock outputBlock, byte[] bytes, int index)
   {
      float value = BitConverter.ToSingle(bytes, index);

      outputBlock.Text += String.Format(formatter, index,
          BitConverter.ToString(bytes, index, 4), value) + "\n";
   }

   // Display a byte array, using multiple lines if necessary.
   public static void WriteMultiLineByteArray(System.Windows.Controls.TextBlock outputBlock, byte[] bytes)
   {
      const int rowSize = 20;
      int iter;

      outputBlock.Text += "initial byte array" + "\n";
      outputBlock.Text += "------------------" + "\n";

      for (iter = 0; iter < bytes.Length - rowSize; iter += rowSize)
      {
         outputBlock.Text += String.Format(
             BitConverter.ToString(bytes, iter, rowSize));
         outputBlock.Text += "-" + "\n";
      }

      outputBlock.Text += String.Format(BitConverter.ToString(bytes, iter)) + "\n";
      outputBlock.Text += "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      byte[] byteArray = {
              0,   0,   0,   0, 128,  63,   0,   0, 112,  65, 
              0, 255, 127,  71,   0,   0, 128,  59,   0,   0, 
            128,  47,  73,  70, 131,   5,  75,   6, 158,  63, 
             77,   6, 158,  63,  80,   6, 158,  63,  30,  55, 
            190, 121, 255, 255, 127, 255, 255, 127, 127,   1, 
              0,   0,   0, 192, 255,   0,   0, 128, 255,   0, 
              0, 128, 127 };

      outputBlock.Text += String.Format(
          "This example of the BitConverter.ToSingle( byte( ), " +
          "int ) \nmethod generates the following output. It " +
          "converts elements \nof a byte array to float values.\n") + "\n";

      WriteMultiLineByteArray(outputBlock, byteArray);

      outputBlock.Text += String.Format(formatter, "index", "array elements",
          "float") + "\n";
      outputBlock.Text += String.Format(formatter, "-----", "--------------",
          "-----") + "\n";

      // Convert byte array elements to float values.
      BAToSingle(outputBlock, byteArray, 0);
      BAToSingle(outputBlock, byteArray, 2);
      BAToSingle(outputBlock, byteArray, 6);
      BAToSingle(outputBlock, byteArray, 10);
      BAToSingle(outputBlock, byteArray, 14);
      BAToSingle(outputBlock, byteArray, 18);
      BAToSingle(outputBlock, byteArray, 22);
      BAToSingle(outputBlock, byteArray, 26);
      BAToSingle(outputBlock, byteArray, 30);
      BAToSingle(outputBlock, byteArray, 34);
      BAToSingle(outputBlock, byteArray, 38);
      BAToSingle(outputBlock, byteArray, 42);
      BAToSingle(outputBlock, byteArray, 45);
      BAToSingle(outputBlock, byteArray, 49);
      BAToSingle(outputBlock, byteArray, 51);
      BAToSingle(outputBlock, byteArray, 55);
      BAToSingle(outputBlock, byteArray, 59);
   }
}

/*
This example of the BitConverter.ToSingle( byte( ), int )
method generates the following output. It converts elements
of a byte array to float values.

initial byte array
------------------
00-00-00-00-80-3F-00-00-70-41-00-FF-7F-47-00-00-80-3B-00-00-
80-2F-49-46-83-05-4B-06-9E-3F-4D-06-9E-3F-50-06-9E-3F-1E-37-
BE-79-FF-FF-7F-FF-FF-7F-7F-01-00-00-00-C0-FF-00-00-80-FF-00-
00-80-7F

index   array elements             float
-----   --------------             -----
    0      00-00-00-00    0.0000000E+000
    2      00-00-80-3F    1.0000000E+000
    6      00-00-70-41    1.5000000E+001
   10      00-FF-7F-47    6.5535000E+004
   14      00-00-80-3B    3.9062500E-003
   18      00-00-80-2F    2.3283064E-010
   22      49-46-83-05    1.2345000E-035
   26      4B-06-9E-3F    1.2345671E+000
   30      4D-06-9E-3F    1.2345673E+000
   34      50-06-9E-3F    1.2345676E+000
   38      1E-37-BE-79    1.2345679E+035
   42      FF-FF-7F-FF   -3.4028235E+038
   45      FF-FF-7F-7F    3.4028235E+038
   49      01-00-00-00    1.4012985E-045
   51      00-00-C0-FF               NaN
   55      00-00-80-FF         -Infinity
   59      00-00-80-7F          Infinity
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: