Partager via


BitConverter.GetBytes Method (Double)

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

Returns the specified double-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 Double _
) As Byte()
[SecuritySafeCriticalAttribute]
public static byte[] GetBytes(
    double value
)

Parameters

Return Value

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

Examples

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

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

Module Example

   Const formatter As String = "{0,25:E16}{1,30}"

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

      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( Double ) " & _
          vbCrLf & "method generates the following " & _
          "output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Double", "Byte array") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "------", "----------") & vbCrLf

      ' Convert Double values and display the results.
      GetBytesDouble(outputBlock, 0.0)
      GetBytesDouble(outputBlock, 1.0)
      GetBytesDouble(outputBlock, 255.0)
      GetBytesDouble(outputBlock, 4294967295.0)
      GetBytesDouble(outputBlock, 0.00390625)
      GetBytesDouble(outputBlock, 0.00000000023283064365386963)
      GetBytesDouble(outputBlock, 1.23456789012345E-300)
      GetBytesDouble(outputBlock, 1.2345678901234565)
      GetBytesDouble(outputBlock, 1.2345678901234567)
      GetBytesDouble(outputBlock, 1.2345678901234569)
      GetBytesDouble(outputBlock, 1.2345678901234568E+300)
      GetBytesDouble(outputBlock, Double.MinValue)
      GetBytesDouble(outputBlock, Double.MaxValue)
      GetBytesDouble(outputBlock, Double.Epsilon)
      GetBytesDouble(outputBlock, Double.NaN)
      GetBytesDouble(outputBlock, Double.NegativeInfinity)
      GetBytesDouble(outputBlock, Double.PositiveInfinity)
   End Sub
End Module

' This example of the BitConverter.GetBytes( Double )
' method generates the following output.
' 
'                    Double                    Byte array
'                    ------                    ----------
'   0.0000000000000000E+000       00-00-00-00-00-00-00-00
'   1.0000000000000000E+000       00-00-00-00-00-00-F0-3F
'   2.5500000000000000E+002       00-00-00-00-00-E0-6F-40
'   4.2949672950000000E+009       00-00-E0-FF-FF-FF-EF-41
'   3.9062500000000000E-003       00-00-00-00-00-00-70-3F
'   2.3283064365386963E-010       00-00-00-00-00-00-F0-3D
'   1.2345678901234500E-300       DF-88-1E-1C-FE-74-AA-01
'   1.2345678901234565E+000       FA-59-8C-42-CA-C0-F3-3F
'   1.2345678901234567E+000       FB-59-8C-42-CA-C0-F3-3F
'   1.2345678901234569E+000       FC-59-8C-42-CA-C0-F3-3F
'   1.2345678901234569E+300       52-D3-BB-BC-E8-7E-3D-7E
'  -1.7976931348623157E+308       FF-FF-FF-FF-FF-FF-EF-FF
'   1.7976931348623157E+308       FF-FF-FF-FF-FF-FF-EF-7F
'   4.9406564584124654E-324       01-00-00-00-00-00-00-00
'                       NaN       00-00-00-00-00-00-F8-FF
'                 -Infinity       00-00-00-00-00-00-F0-FF
'                  Infinity       00-00-00-00-00-00-F0-7F
// Example of the BitConverter.GetBytes( double ) method.
using System;

class Example
{
   const string formatter = "{0,25:E16}{1,30}";

   // Convert a double argument to a byte array and display it.
   public static void GetBytesDouble(System.Windows.Controls.TextBlock outputBlock, double 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( double ) " +
          "\nmethod generates the following output.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "double", "byte array") + "\n";
      outputBlock.Text += String.Format(formatter, "------", "----------") + "\n";

      // Convert double values and display the results.
      GetBytesDouble(outputBlock, 0.0);
      GetBytesDouble(outputBlock, 1.0);
      GetBytesDouble(outputBlock, 255.0);
      GetBytesDouble(outputBlock, 4294967295.0);
      GetBytesDouble(outputBlock, 0.00390625);
      GetBytesDouble(outputBlock, 0.00000000023283064365386962890625);
      GetBytesDouble(outputBlock, 1.23456789012345E-300);
      GetBytesDouble(outputBlock, 1.2345678901234565);
      GetBytesDouble(outputBlock, 1.2345678901234567);
      GetBytesDouble(outputBlock, 1.2345678901234569);
      GetBytesDouble(outputBlock, 1.23456789012345678E+300);
      GetBytesDouble(outputBlock, double.MinValue);
      GetBytesDouble(outputBlock, double.MaxValue);
      GetBytesDouble(outputBlock, double.Epsilon);
      GetBytesDouble(outputBlock, double.NaN);
      GetBytesDouble(outputBlock, double.NegativeInfinity);
      GetBytesDouble(outputBlock, double.PositiveInfinity);
   }
}

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

                   double                    byte array
                   ------                    ----------
  0.0000000000000000E+000       00-00-00-00-00-00-00-00
  1.0000000000000000E+000       00-00-00-00-00-00-F0-3F
  2.5500000000000000E+002       00-00-00-00-00-E0-6F-40
  4.2949672950000000E+009       00-00-E0-FF-FF-FF-EF-41
  3.9062500000000000E-003       00-00-00-00-00-00-70-3F
  2.3283064365386963E-010       00-00-00-00-00-00-F0-3D
  1.2345678901234500E-300       DF-88-1E-1C-FE-74-AA-01
  1.2345678901234565E+000       FA-59-8C-42-CA-C0-F3-3F
  1.2345678901234567E+000       FB-59-8C-42-CA-C0-F3-3F
  1.2345678901234569E+000       FC-59-8C-42-CA-C0-F3-3F
  1.2345678901234569E+300       52-D3-BB-BC-E8-7E-3D-7E
 -1.7976931348623157E+308       FF-FF-FF-FF-FF-FF-EF-FF
  1.7976931348623157E+308       FF-FF-FF-FF-FF-FF-EF-7F
  4.9406564584124654E-324       01-00-00-00-00-00-00-00
                      NaN       00-00-00-00-00-00-F8-FF
                -Infinity       00-00-00-00-00-00-F0-FF
                 Infinity       00-00-00-00-00-00-F0-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.