Aracılığıyla paylaş


BitConverter.ToDouble Method

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

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

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function ToDouble ( _
    value As Byte(), _
    startIndex As Integer _
) As Double
[SecuritySafeCriticalAttribute]
public static double ToDouble(
    byte[] value,
    int startIndex
)

Parameters

  • startIndex
    Type: System.Int32
    The starting position within value.

Return Value

Type: System.Double
A double precision floating point number formed by eight bytes beginning at startIndex.

Exceptions

Exception Condition
ArgumentException

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

ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

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

Examples

The following code example converts elements of Byte arrays to Double values with the ToDouble method.

' Example of the BitConverter.ToDouble method.

Module Example

   Const formatter As String = "{0,5}{1,27}{2,27:E16}"

   ' Convert eight Byte array elements to a Double and display it.
   Sub BAToDouble(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal bytes() As Byte, ByVal index As Integer)

      Dim value As Double = BitConverter.ToDouble(bytes, index)

      outputBlock.Text &= String.Format(formatter, index, _
          BitConverter.ToString(bytes, index, 8), value) & vbCrLf
   End Sub

   ' Display a Byte array, using multiple lines if necessary.
   Sub WriteMultiLineByteArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal bytes() As Byte)

      Const rowSize As Integer = 20
      Dim iter As Integer

      outputBlock.Text &= "initial Byte array" & vbCrLf
      outputBlock.Text &= "------------------" & vbCrLf

      For iter = 0 To bytes.Length - rowSize - 1 Step rowSize
         outputBlock.Text &= String.Format( _
             BitConverter.ToString(bytes, iter, rowSize))
         outputBlock.Text &= "-" & vbCrLf
      Next iter

      outputBlock.Text &= String.Format(BitConverter.ToString(bytes, iter)) & vbCrLf
      outputBlock.Text &= vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim byteArray As Byte() = { _
            0, 0, 0, 0, 0, 0, 0, 0, 240, 63, _
            0, 0, 0, 0, 0, 224, 111, 64, 0, 0, _
          224, 255, 255, 255, 239, 65, 0, 0, 0, 0, _
            0, 0, 112, 63, 0, 0, 0, 0, 0, 0, _
          240, 61, 223, 136, 30, 28, 254, 116, 170, 1, _
          250, 89, 140, 66, 202, 192, 243, 63, 251, 89, _
          140, 66, 202, 192, 243, 63, 252, 89, 140, 66, _
          202, 192, 243, 63, 82, 211, 187, 188, 232, 126, _
           61, 126, 255, 255, 255, 255, 255, 255, 239, 255, _
          255, 255, 255, 255, 255, 239, 127, 1, 0, 0, _
            0, 0, 0, 0, 0, 248, 255, 0, 0, 0, _
            0, 0, 0, 240, 255, 0, 0, 0, 0, 0, _
            0, 240, 127}

      outputBlock.Text &= String.Format( _
          "This example of the BitConverter.ToDouble( Byte( ), " & _
          "Integer ) " & vbCrLf & "method generates the " & _
          "following output. It converts elements " & vbCrLf & _
          "of a Byte array to Double values." & vbCrLf) & vbCrLf

      WriteMultiLineByteArray(outputBlock, byteArray)

      outputBlock.Text &= String.Format(formatter, "index", "array elements", _
          "Double") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "-----", "--------------", _
          "------") & vbCrLf

      ' Convert Byte array elements to Double values.
      BAToDouble(outputBlock, byteArray, 0)
      BAToDouble(outputBlock, byteArray, 2)
      BAToDouble(outputBlock, byteArray, 10)
      BAToDouble(outputBlock, byteArray, 18)
      BAToDouble(outputBlock, byteArray, 26)
      BAToDouble(outputBlock, byteArray, 34)
      BAToDouble(outputBlock, byteArray, 42)
      BAToDouble(outputBlock, byteArray, 50)
      BAToDouble(outputBlock, byteArray, 58)
      BAToDouble(outputBlock, byteArray, 66)
      BAToDouble(outputBlock, byteArray, 74)
      BAToDouble(outputBlock, byteArray, 82)
      BAToDouble(outputBlock, byteArray, 89)
      BAToDouble(outputBlock, byteArray, 97)
      BAToDouble(outputBlock, byteArray, 99)
      BAToDouble(outputBlock, byteArray, 107)
      BAToDouble(outputBlock, byteArray, 115)
   End Sub
End Module

' This example of the BitConverter.ToDouble( Byte( ), Integer )
' method generates the following output. It converts elements
' of a Byte array to Double values.
' 
' initial Byte array
' ------------------
' 00-00-00-00-00-00-00-00-F0-3F-00-00-00-00-00-E0-6F-40-00-00-
' E0-FF-FF-FF-EF-41-00-00-00-00-00-00-70-3F-00-00-00-00-00-00-
' F0-3D-DF-88-1E-1C-FE-74-AA-01-FA-59-8C-42-CA-C0-F3-3F-FB-59-
' 8C-42-CA-C0-F3-3F-FC-59-8C-42-CA-C0-F3-3F-52-D3-BB-BC-E8-7E-
' 3D-7E-FF-FF-FF-FF-FF-FF-EF-FF-FF-FF-FF-FF-FF-EF-7F-01-00-00-
' 00-00-00-00-00-F8-FF-00-00-00-00-00-00-F0-FF-00-00-00-00-00-
' 00-F0-7F
' 
' index             array elements                     Double
' -----             --------------                     ------
'     0    00-00-00-00-00-00-00-00    0.0000000000000000E+000
'     2    00-00-00-00-00-00-F0-3F    1.0000000000000000E+000
'    10    00-00-00-00-00-E0-6F-40    2.5500000000000000E+002
'    18    00-00-E0-FF-FF-FF-EF-41    4.2949672950000000E+009
'    26    00-00-00-00-00-00-70-3F    3.9062500000000000E-003
'    34    00-00-00-00-00-00-F0-3D    2.3283064365386963E-010
'    42    DF-88-1E-1C-FE-74-AA-01    1.2345678901234500E-300
'    50    FA-59-8C-42-CA-C0-F3-3F    1.2345678901234565E+000
'    58    FB-59-8C-42-CA-C0-F3-3F    1.2345678901234567E+000
'    66    FC-59-8C-42-CA-C0-F3-3F    1.2345678901234569E+000
'    74    52-D3-BB-BC-E8-7E-3D-7E    1.2345678901234569E+300
'    82    FF-FF-FF-FF-FF-FF-EF-FF   -1.7976931348623157E+308
'    89    FF-FF-FF-FF-FF-FF-EF-7F    1.7976931348623157E+308
'    97    01-00-00-00-00-00-00-00    4.9406564584124654E-324
'    99    00-00-00-00-00-00-F8-FF                        NaN
'   107    00-00-00-00-00-00-F0-FF                  -Infinity
'   115    00-00-00-00-00-00-F0-7F                   Infinity
// Example of the BitConverter.ToDouble method.
using System;

class Example
{
   const string formatter = "{0,5}{1,27}{2,27:E16}";

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

      outputBlock.Text += String.Format(formatter, index,
          BitConverter.ToString(bytes, index, 8), 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,   0,   0,   0,   0, 240,  63, 
              0,   0,   0,   0,   0, 224, 111,  64,   0,   0, 
            224, 255, 255, 255, 239,  65,   0,   0,   0,   0, 
              0,   0, 112,  63,   0,   0,   0,   0,   0,   0, 
            240,  61, 223, 136,  30,  28, 254, 116, 170,   1, 
            250,  89, 140,  66, 202, 192, 243,  63, 251,  89, 
            140,  66, 202, 192, 243,  63, 252,  89, 140,  66, 
            202, 192, 243,  63,  82, 211, 187, 188, 232, 126, 
             61, 126, 255, 255, 255, 255, 255, 255, 239, 255, 
            255, 255, 255, 255, 255, 239, 127,   1,   0,   0, 
              0,   0,   0,   0,   0, 248, 255,   0,   0,   0, 
              0,   0,   0, 240, 255,   0,   0,   0,   0,   0, 
              0, 240, 127 };

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

      WriteMultiLineByteArray(outputBlock, byteArray);

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

      // Convert byte array elements to double values.
      BAToDouble(outputBlock, byteArray, 0);
      BAToDouble(outputBlock, byteArray, 2);
      BAToDouble(outputBlock, byteArray, 10);
      BAToDouble(outputBlock, byteArray, 18);
      BAToDouble(outputBlock, byteArray, 26);
      BAToDouble(outputBlock, byteArray, 34);
      BAToDouble(outputBlock, byteArray, 42);
      BAToDouble(outputBlock, byteArray, 50);
      BAToDouble(outputBlock, byteArray, 58);
      BAToDouble(outputBlock, byteArray, 66);
      BAToDouble(outputBlock, byteArray, 74);
      BAToDouble(outputBlock, byteArray, 82);
      BAToDouble(outputBlock, byteArray, 89);
      BAToDouble(outputBlock, byteArray, 97);
      BAToDouble(outputBlock, byteArray, 99);
      BAToDouble(outputBlock, byteArray, 107);
      BAToDouble(outputBlock, byteArray, 115);
   }
}

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

initial byte array
------------------
00-00-00-00-00-00-00-00-F0-3F-00-00-00-00-00-E0-6F-40-00-00-
E0-FF-FF-FF-EF-41-00-00-00-00-00-00-70-3F-00-00-00-00-00-00-
F0-3D-DF-88-1E-1C-FE-74-AA-01-FA-59-8C-42-CA-C0-F3-3F-FB-59-
8C-42-CA-C0-F3-3F-FC-59-8C-42-CA-C0-F3-3F-52-D3-BB-BC-E8-7E-
3D-7E-FF-FF-FF-FF-FF-FF-EF-FF-FF-FF-FF-FF-FF-EF-7F-01-00-00-
00-00-00-00-00-F8-FF-00-00-00-00-00-00-F0-FF-00-00-00-00-00-
00-F0-7F

index             array elements                     double
-----             --------------                     ------
    0    00-00-00-00-00-00-00-00    0.0000000000000000E+000
    2    00-00-00-00-00-00-F0-3F    1.0000000000000000E+000
   10    00-00-00-00-00-E0-6F-40    2.5500000000000000E+002
   18    00-00-E0-FF-FF-FF-EF-41    4.2949672950000000E+009
   26    00-00-00-00-00-00-70-3F    3.9062500000000000E-003
   34    00-00-00-00-00-00-F0-3D    2.3283064365386963E-010
   42    DF-88-1E-1C-FE-74-AA-01    1.2345678901234500E-300
   50    FA-59-8C-42-CA-C0-F3-3F    1.2345678901234565E+000
   58    FB-59-8C-42-CA-C0-F3-3F    1.2345678901234567E+000
   66    FC-59-8C-42-CA-C0-F3-3F    1.2345678901234569E+000
   74    52-D3-BB-BC-E8-7E-3D-7E    1.2345678901234569E+300
   82    FF-FF-FF-FF-FF-FF-EF-FF   -1.7976931348623157E+308
   89    FF-FF-FF-FF-FF-FF-EF-7F    1.7976931348623157E+308
   97    01-00-00-00-00-00-00-00    4.9406564584124654E-324
   99    00-00-00-00-00-00-F8-FF                        NaN
  107    00-00-00-00-00-00-F0-FF                  -Infinity
  115    00-00-00-00-00-00-F0-7F                   Infinity
*/

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.