Compartir a través de


BitConverter.GetBytes Method (Single)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the specified single-precision floating point value as an array of bytes.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function GetBytes ( _
    value As Single _
) As Byte()
[SecuritySafeCriticalAttribute]
public static byte[] GetBytes(
    float value
)

Parameters

Return Value

Type: array<System.Byte[]
An array of bytes with length 4.

Examples

The following code example converts the bit patterns of Single values to Byte arrays with the GetBytes method.

' Example of the BitConverter.GetBytes( Single ) method.

Module Example

   Const formatter As String = "{0,16:E7}{1,20}"

   ' Convert a Single argument to a Byte array and display it.
   Sub GetBytesSingle(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Single)

      Dim byteArray As Byte() = BitConverter.GetBytes(argument)
      outputBlock.Text &= String.Format(formatter, argument, _
          BitConverter.ToString(byteArray)) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.GetBytes( Single ) " & _
          vbCrLf & "method generates the following " & _
          "output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Single", "Byte array") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "------", "----------") & vbCrLf

      ' Convert Single values and display the results.
      GetBytesSingle(outputBlock, 0.0F)
      GetBytesSingle(outputBlock, 1.0F)
      GetBytesSingle(outputBlock, 15.0F)
      GetBytesSingle(outputBlock, 65535.0F)
      GetBytesSingle(outputBlock, 0.00390625F)
      GetBytesSingle(outputBlock, 0.000000000232830644F)
      GetBytesSingle(outputBlock, 1.2345E-35F)
      GetBytesSingle(outputBlock, 1.234567F)
      GetBytesSingle(outputBlock, 1.23456728F)
      GetBytesSingle(outputBlock, 1.23456764F)
      GetBytesSingle(outputBlock, 1.23456789E+35F)
      GetBytesSingle(outputBlock, Single.MinValue)
      GetBytesSingle(outputBlock, Single.MaxValue)
      GetBytesSingle(outputBlock, Single.Epsilon)
      GetBytesSingle(outputBlock, Single.NaN)
      GetBytesSingle(outputBlock, Single.NegativeInfinity)
      GetBytesSingle(outputBlock, Single.PositiveInfinity)
   End Sub
End Module

' This example of the BitConverter.GetBytes( Single )
' method generates the following output.
' 
'           Single          Byte array
'           ------          ----------
'   0.0000000E+000         00-00-00-00
'   1.0000000E+000         00-00-80-3F
'   1.5000000E+001         00-00-70-41
'   6.5535000E+004         00-FF-7F-47
'   3.9062500E-003         00-00-80-3B
'   2.3283064E-010         00-00-80-2F
'   1.2345000E-035         49-46-83-05
'   1.2345671E+000         4B-06-9E-3F
'   1.2345673E+000         4D-06-9E-3F
'   1.2345676E+000         50-06-9E-3F
'   1.2345679E+035         1E-37-BE-79
'  -3.4028235E+038         FF-FF-7F-FF
'   3.4028235E+038         FF-FF-7F-7F
'   1.4012985E-045         01-00-00-00
'              NaN         00-00-C0-FF
'        -Infinity         00-00-80-FF
'         Infinity         00-00-80-7F
// Example of the BitConverter.GetBytes( float ) method.
using System;

class Example
{
   const string formatter = "{0,16:E7}{1,20}";

   // Convert a float argument to a byte array and display it.
   public static void GetBytesSingle(System.Windows.Controls.TextBlock outputBlock, float argument)
   {
      byte[] byteArray = BitConverter.GetBytes(argument);
      outputBlock.Text += String.Format(formatter, argument,
          BitConverter.ToString(byteArray)) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format(
          "This example of the BitConverter.GetBytes( float ) " +
          "\nmethod generates the following output.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "float", "byte array") + "\n";
      outputBlock.Text += String.Format(formatter, "-----", "----------") + "\n";

      // Convert float values and display the results.
      GetBytesSingle(outputBlock, 0.0F);
      GetBytesSingle(outputBlock, 1.0F);
      GetBytesSingle(outputBlock, 15.0F);
      GetBytesSingle(outputBlock, 65535.0F);
      GetBytesSingle(outputBlock, 0.00390625F);
      GetBytesSingle(outputBlock, 0.00000000023283064365386962890625F);
      GetBytesSingle(outputBlock, 1.2345E-35F);
      GetBytesSingle(outputBlock, 1.2345671F);
      GetBytesSingle(outputBlock, 1.2345673F);
      GetBytesSingle(outputBlock, 1.2345677F);
      GetBytesSingle(outputBlock, 1.23456789E+35F);
      GetBytesSingle(outputBlock, float.MinValue);
      GetBytesSingle(outputBlock, float.MaxValue);
      GetBytesSingle(outputBlock, float.Epsilon);
      GetBytesSingle(outputBlock, float.NaN);
      GetBytesSingle(outputBlock, float.NegativeInfinity);
      GetBytesSingle(outputBlock, float.PositiveInfinity);
   }
}

/*
This example of the BitConverter.GetBytes( float )
method generates the following output.

           float          byte array
           -----          ----------
  0.0000000E+000         00-00-00-00
  1.0000000E+000         00-00-80-3F
  1.5000000E+001         00-00-70-41
  6.5535000E+004         00-FF-7F-47
  3.9062500E-003         00-00-80-3B
  2.3283064E-010         00-00-80-2F
  1.2345000E-035         49-46-83-05
  1.2345671E+000         4B-06-9E-3F
  1.2345673E+000         4D-06-9E-3F
  1.2345676E+000         50-06-9E-3F
  1.2345679E+035         1E-37-BE-79
 -3.4028235E+038         FF-FF-7F-FF
  3.4028235E+038         FF-FF-7F-7F
  1.4012985E-045         01-00-00-00
             NaN         00-00-C0-FF
       -Infinity         00-00-80-FF
        Infinity         00-00-80-7F
*/

Version Information

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

Platforms

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