BitConverter.GetBytes 方法

定義

將指定的資料轉換成位元組陣列。

多載

GetBytes(Boolean)

傳回指定的布林值為位元組陣列。

GetBytes(Char)

傳回指定的 Unicode 位元值為位元組陣列。

GetBytes(Double)

傳回指定的雙精確度浮點數做為位元組陣列。

GetBytes(Half)

將指定的半精確度浮點值當做位元組陣列傳回。

GetBytes(Int16)

傳回指定的 16 位元帶正負號的整數值為位元組陣列。

GetBytes(Int32)

以位元組陣列形式傳回指定的 32 位元帶正負號整數值。

GetBytes(Int64)

以位元組陣列形式傳回指定的 64 位元帶正負號整數值。

GetBytes(Single)

傳回指定的單精確度浮點數值為位元組陣列。

GetBytes(UInt16)

傳回指定的 16 位元不帶正負號的整數值為位元組陣列。

GetBytes(UInt32)

傳回指定的 32 位元不帶正負號的整數值為位元組陣列。

GetBytes(UInt64)

傳回指定的 64 位元不帶正負號的整數值為位元組陣列。

GetBytes(Boolean)

傳回指定的布林值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(bool value);
public static byte[] GetBytes (bool value);
static member GetBytes : bool -> byte[]
Public Shared Function GetBytes (value As Boolean) As Byte()

參數

value
Boolean

要進行轉換的值。

傳回

Byte[]

長度為 1 的位元組陣列。

範例

下列範例會使用 GetBytes 方法,將值的位模式 Boolean 轉換為 Byte 陣列。

using namespace System;

int main()
{
    // Define Boolean true and false values.
    array<bool>^ values = { true, false };

    // Display the value and its corresponding byte array.
    Console::WriteLine("{0,10}{1,16}\n", "Boolean", "Bytes");
    for each (Byte value in values) {
       array<Byte>^ bytes = BitConverter::GetBytes(value); 
       Console::WriteLine("{0,10}{1,16}", value, 
                          BitConverter::ToString(bytes));
    }
}
// This example displays the following output:
//        Boolean           Bytes
//     
//           True              01
//          False              00
using System;

class Example
{
   public static void Main( )
   {
      // Define Boolean true and false values.
      bool[] values = { true, false };

      // Display the value and its corresponding byte array.
      Console.WriteLine("{0,10}{1,16}\n", "Boolean", "Bytes");
      foreach (var value in values) {
         byte[] bytes = BitConverter.GetBytes(value);
         Console.WriteLine("{0,10}{1,16}", value,
                           BitConverter.ToString(bytes));
      }
   }
}
// The example displays the following output:
//        Boolean           Bytes
//
//           True              01
//          False              00
open System

// Define Boolean true and false values.
let values = [ true; false ]

// Display the value and its corresponding byte array.
printfn "%10s%16s\n" "Boolean" "Bytes"

for value in values do
    let bytes = BitConverter.GetBytes value
    
    BitConverter.ToString bytes
    |> printfn "%10b%16s" value


// The example displays the following output:
//        Boolean           Bytes
//
//           true              01
//          false              00
Module Example
   Public Sub Main()
      ' Define Boolean true and false values.
      Dim values() As Boolean = { true, false }

      ' Display the value and its corresponding byte array.
      Console.WriteLine("{0,10}{1,16}", "Boolean", "Bytes")
      Console.WriteLine()
      
      For Each value In values
         Dim bytes() As Byte = BitConverter.GetBytes(value) 
         Console.WriteLine("{0,10}{1,16}", value, 
                           BitConverter.ToString(bytes))
      Next
    End Sub 
End Module
' The example displays the following output:
'        Boolean           Bytes
'     
'           True              01
'          False              00

備註

您可以呼叫 ToBoolean 方法,將位元組陣列轉換回 Boolean 值。

另請參閱

適用於

GetBytes(Char)

傳回指定的 Unicode 位元值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(char value);
public static byte[] GetBytes (char value);
static member GetBytes : char -> byte[]
Public Shared Function GetBytes (value As Char) As Byte()

參數

value
Char

要轉換的字元。

傳回

Byte[]

長度為 2 的位元組陣列。

範例

下列程式碼範例會使用 方法,將 unicode 字元 () 值的位模式 Char 轉換為 Byte 陣列 GetBytes

// Example of the BitConverter::GetBytes( __wchar_t ) method.
using namespace System;

// Convert a __wchar_t argument to a byte array and display it.
void GetBytesChar( __wchar_t argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,10}{1,16}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( __wchar_t ) "
   "\nmethod generates the following output.\n" );
   Console::WriteLine( "{0,10}{1,16}", "__wchar_t", "byte array" );
   Console::WriteLine( "{0,10}{1,16}", "---------", "----------" );
   
   // Convert __wchar_t values and display the results.
   GetBytesChar( L'\0' );
   GetBytesChar( L' ' );
   GetBytesChar( L'*' );
   GetBytesChar( L'3' );
   GetBytesChar( L'A' );
   GetBytesChar( L'[' );
   GetBytesChar( L'a' );
   GetBytesChar( L'{' );
}

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

 __wchar_t      byte array
 ---------      ----------
                     00-00
                     20-00
         *           2A-00
         3           33-00
         A           41-00
         [           5B-00
         a           61-00
         {           7B-00
*/
// Example of the BitConverter.GetBytes( char ) method.
using System;

class GetBytesCharDemo
{
    const string formatter = "{0,10}{1,16}";

    // Convert a char argument to a byte array and display it.
    public static void GetBytesChar( char argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( char ) " +
            "\nmethod generates the following output.\r\n" );
        Console.WriteLine( formatter, "char", "byte array" );
        Console.WriteLine( formatter, "----", "----------" );

        // Convert char values and display the results.
        GetBytesChar( '\0' );
        GetBytesChar( ' ' );
        GetBytesChar( '*' );
        GetBytesChar( '3' );
        GetBytesChar( 'A' );
        GetBytesChar( '[' );
        GetBytesChar( 'a' );
        GetBytesChar( '{' );
    }
}

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

      char      byte array
      ----      ----------
                     00-00
                     20-00
         *           2A-00
         3           33-00
         A           41-00
         [           5B-00
         a           61-00
         {           7B-00
*/
open System

let print obj1 obj2 =
    printfn $"{obj1,10}{obj2,16}"

// Convert a char argument to a byte array and display it.
let getBytesChar (argument: char) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(char) \nmethod generates the following output.\r\n"
print "char" "byte array"
print "----" "----------"

// Convert char values and display the results.
getBytesChar '\000'
getBytesChar ' '
getBytesChar '*'
getBytesChar '3'
getBytesChar 'A'
getBytesChar '['
getBytesChar 'a'
getBytesChar '{'


// This example of the BitConverter.GetBytes(char)
// method generates the following output.
//
//       char      byte array
//       ----      ----------
//                      00-00
//                      20-00
//          *           2A-00
//          3           33-00
//          A           41-00
//          [           5B-00
//          a           61-00
//          {           7B-00
' Example of the BitConverter.GetBytes( Char ) method.
Module GetBytesCharDemo

    Const formatter As String = "{0,10}{1,16}"
 
    ' Convert a Char argument to a Byte array and display it.
    Sub GetBytesChar( argument As Char )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( Char ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Char", "Byte array" )
        Console.WriteLine( formatter, "----", "----------" )
          
        ' Convert Char values and display the results.
        GetBytesChar( Chr( 0 ) )
        GetBytesChar( " "c )
        GetBytesChar( "*"c )
        GetBytesChar( "3"c )
        GetBytesChar( "A"c )
        GetBytesChar( "["c )
        GetBytesChar( "a"c )
        GetBytesChar( "{"c )
    End Sub 
End Module

' This example of the BitConverter.GetBytes( Char )
' method generates the following output.
' 
'       Char      Byte array
'       ----      ----------
'                      00-00
'                      20-00
'          *           2A-00
'          3           33-00
'          A           41-00
'          [           5B-00
'          a           61-00
'          {           7B-00

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(Double)

傳回指定的雙精確度浮點數做為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(double value);
public static byte[] GetBytes (double value);
static member GetBytes : double -> byte[]
Public Shared Function GetBytes (value As Double) As Byte()

參數

value
Double

要轉換的數字。

傳回

Byte[]

長度為 8 的位元組陣列。

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 Double 轉換為 Byte 陣列。

// Example of the BitConverter::GetBytes( double ) method.
using namespace System;

// Convert a double argument to a byte array and display it.
void GetBytesDouble( double argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,25:E16}{1,30}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( double ) "
   "\nmethod generates the following output.\n" );
   Console::WriteLine( "{0,25:E16}{1,30}", "double", "byte array" );
   Console::WriteLine( "{0,25:E16}{1,30}", "------", "----------" );
   
   // Convert double values and display the results.
   GetBytesDouble( 0.0 );
   GetBytesDouble( 1.0 );
   GetBytesDouble( 255.0 );
   GetBytesDouble( 4294967295.0 );
   GetBytesDouble( 0.00390625 );
   GetBytesDouble( 0.00000000023283064365386962890625 );
   GetBytesDouble( 1.23456789012345E-300 );
   GetBytesDouble( 1.2345678901234565 );
   GetBytesDouble( 1.2345678901234567 );
   GetBytesDouble( 1.2345678901234569 );
   GetBytesDouble( 1.23456789012345678E+300 );
   GetBytesDouble( Double::MinValue );
   GetBytesDouble( Double::MaxValue );
   GetBytesDouble( Double::Epsilon );
   GetBytesDouble( Double::NaN );
   GetBytesDouble( Double::NegativeInfinity );
   GetBytesDouble( 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
*/
// Example of the BitConverter.GetBytes( double ) method.
using System;

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

    // Convert a double argument to a byte array and display it.
    public static void GetBytesDouble( double argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( double ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "double", "byte array" );
        Console.WriteLine( formatter, "------", "----------" );

        // Convert double values and display the results.
        GetBytesDouble( 0.0 );
        GetBytesDouble( 1.0 );
        GetBytesDouble( 255.0 );
        GetBytesDouble( 4294967295.0 );
        GetBytesDouble( 0.00390625 );
        GetBytesDouble( 0.00000000023283064365386962890625 );
        GetBytesDouble( 1.23456789012345E-300 );
        GetBytesDouble( 1.2345678901234565 );
        GetBytesDouble( 1.2345678901234567 );
        GetBytesDouble( 1.2345678901234569 );
        GetBytesDouble( 1.23456789012345678E+300 );
        GetBytesDouble( double.MinValue );
        GetBytesDouble( double.MaxValue );
        GetBytesDouble( double.Epsilon );
        GetBytesDouble( double.NaN );
        GetBytesDouble( double.NegativeInfinity );
        GetBytesDouble( 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
*/
open System

let print obj1 obj2 = printfn $"{obj1,25:E16}{obj2,30}"

// Convert a double argument to a byte array and display it.
let getBytesDouble (argument: float) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(double) \nmethod generates the following output.\n"
print "double" "byte array"
print "------" "----------"

// Convert double values and display the results.
getBytesDouble 0.0
getBytesDouble 1.0
getBytesDouble 255.0
getBytesDouble 4294967295.0
getBytesDouble 0.00390625
getBytesDouble 0.00000000023283064365386962890625
getBytesDouble 1.23456789012345E-300
getBytesDouble 1.2345678901234565
getBytesDouble 1.2345678901234567
getBytesDouble 1.2345678901234569
getBytesDouble 1.23456789012345678E+300
getBytesDouble Double.MinValue
getBytesDouble Double.MaxValue
getBytesDouble Double.Epsilon
getBytesDouble Double.NaN
getBytesDouble Double.NegativeInfinity
getBytesDouble 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
' Example of the BitConverter.GetBytes( Double ) method.
Module GetBytesDoubleDemo

    Const formatter As String = "{0,25:E16}{1,30}"
 
    ' Convert a Double argument to a Byte array and display it.
    Sub GetBytesDouble( argument As Double )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( Double ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Double", "Byte array" )
        Console.WriteLine( formatter, "------", "----------" )
          
        ' Convert Double values and display the results.
        GetBytesDouble( 0.0 )
        GetBytesDouble( 1.0 )
        GetBytesDouble( 255.0 )
        GetBytesDouble( 4294967295.0 )
        GetBytesDouble( 0.00390625 )
        GetBytesDouble( 0.00000000023283064365386962890625 )
        GetBytesDouble( 1.23456789012345E-300 )
        GetBytesDouble( 1.2345678901234565 )
        GetBytesDouble( 1.2345678901234567 )
        GetBytesDouble( 1.2345678901234569 )
        GetBytesDouble( 1.23456789012345678E+300 )
        GetBytesDouble( Double.MinValue )
        GetBytesDouble( Double.MaxValue )
        GetBytesDouble( Double.Epsilon )
        GetBytesDouble( Double.NaN )
        GetBytesDouble( Double.NegativeInfinity )
        GetBytesDouble( 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

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(Half)

將指定的半精確度浮點值當做位元組陣列傳回。

public:
 static cli::array <System::Byte> ^ GetBytes(Half value);
public static byte[] GetBytes (Half value);
static member GetBytes : Half -> byte[]
Public Shared Function GetBytes (value As Half) As Byte()

參數

value
Half

要轉換的數字。

傳回

Byte[]

長度為 2 的位元組陣列。

適用於

GetBytes(Int16)

傳回指定的 16 位元帶正負號的整數值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(short value);
public static byte[] GetBytes (short value);
static member GetBytes : int16 -> byte[]
Public Shared Function GetBytes (value As Short) As Byte()

參數

value
Int16

要轉換的數字。

傳回

Byte[]

長度為 2 的位元組陣列。

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 Int16 轉換為 Byte 陣列。

using namespace System;

void main()
{
    // Define an array of integers.
    array<Int16>^ values = { 0, 15, -15, 10000,  -10000, 
                             Int16::MinValue, Int16::MaxValue};
       
    // Convert each integer to a byte array.
    Console::WriteLine("{0,16}{1,10}{2,17}", "Integer", 
                       "Endian", "Byte Array");
    Console::WriteLine("{0,16}{1,10}{2,17}", "---", "------", 
                       "----------");
    for each (int value in values) {
      array<Byte>^ byteArray = BitConverter::GetBytes(value);
      Console::WriteLine("{0,16}{1,10}{2,17}", value, 
                        BitConverter::IsLittleEndian ? "Little" : " Big", 
                        BitConverter::ToString(byteArray));
    }
}
// This example displays output like the following:
//              Integer    Endian       Byte Array
//                  ---    ------       ----------
//                    0    Little            00-00
//                   15    Little            0F-00
//                  -15    Little            F1-FF
//                10000    Little            10-27
//               -10000    Little            F0-D8
//               -32768    Little            00-80
//                32767    Little            FF-7F
// Example of the BitConverter.GetBytes( short ) method.
using System;

class GetBytesInt16Demo
{
    const string formatter = "{0,10}{1,13}";

    // Convert a short argument to a byte array and display it.
    public static void GetBytesInt16( short argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( short ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "short", "byte array" );
        Console.WriteLine( formatter, "-----", "----------" );

        // Convert short values and display the results.
        GetBytesInt16( 0 );
        GetBytesInt16( 15 );
        GetBytesInt16( -15 );
        GetBytesInt16( 10000 );
        GetBytesInt16( -10000 );
        GetBytesInt16( short.MinValue );
        GetBytesInt16( short.MaxValue );
    }
}

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

     short   byte array
     -----   ----------
         0        00-00
        15        0F-00
       -15        F1-FF
     10000        10-27
    -10000        F0-D8
    -32768        00-80
     32767        FF-7F
*/
open System

let print obj1 obj2 = printfn $"{obj1,10}{obj2,13}"

// Convert a short argument to a byte array and display it.
let getBytesInt16 (argument: int16) =
    let byteArray = BitConverter.GetBytes argument

    BitConverter.ToString byteArray
    |> print argument 

printfn "This example of the BitConverter.GetBytes(int16) \nmethod generates the following output.\n"
print "short" "byte array"
print "-----" "----------"

// Convert short values and display the results.
getBytesInt16 0s
getBytesInt16 15s
getBytesInt16 -15s
getBytesInt16 10000s
getBytesInt16 -10000s
getBytesInt16 Int16.MinValue
getBytesInt16 Int16.MaxValue


// This example of the BitConverter.GetBytes(int16)
// method generates the following output.
//
//      short   byte array
//      -----   ----------
//          0        00-00
//         15        0F-00
//        -15        F1-FF
//      10000        10-27
//     -10000        F0-D8
//     -32768        00-80
//      32767        FF-7F
Module Example
    Public Sub Main( )
        ' Define an array of integers.
        Dim values() As Integer  = { 0, 15, -15, 10000,  -10000, 
                                     Short.MinValue, Short.MaxValue }
          
        ' Convert each integer to a byte array.
        Console.WriteLine("{0,16}{1,10}{2,17}", "Integer", 
                          "Endian", "Byte Array")
        Console.WriteLine("{0,16}{1,10}{2,17}", "---", "------", 
                          "----------" )
        For Each value In values
          Dim byteArray() As Byte = BitConverter.GetBytes(value)
          Console.WriteLine("{0,16}{1,10}{2,17}", value, 
                            If(BitConverter.IsLittleEndian, "Little", " Big"), 
                            BitConverter.ToString(byteArray))
        Next
    End Sub
End Module
' This example displays output like the following:
'              Integer    Endian       Byte Array
'                  ---    ------       ----------
'                    0    Little            00-00
'                   15    Little            0F-00
'                  -15    Little            F1-FF
'                10000    Little            10-27
'               -10000    Little            F0-D8
'               -32768    Little            00-80
'                32767    Little            FF-7F

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(Int32)

以位元組陣列形式傳回指定的 32 位元帶正負號整數值。

public:
 static cli::array <System::Byte> ^ GetBytes(int value);
public static byte[] GetBytes (int value);
static member GetBytes : int -> byte[]
Public Shared Function GetBytes (value As Integer) As Byte()

參數

value
Int32

要轉換的數字。

傳回

Byte[]

長度為 4 的位元組陣列。

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 Int32 轉換為 Byte 陣列。

using namespace System;

void main()
{
    // Define an array of integers.
    array<int>^ values = { 0, 15, -15, 0x100000,  -0x100000, 1000000000,
                          -1000000000, Int32::MinValue, Int32::MaxValue };
       
    // Convert each integer to a byte array.
    Console::WriteLine("{0,16}{1,10}{2,17}", "Integer", 
                       "Endian", "Byte Array");
    Console::WriteLine("{0,16}{1,10}{2,17}", "---", "------", 
                       "----------" );
    for each (int value in values) {
      array<Byte>^ byteArray = BitConverter::GetBytes(value);
      Console::WriteLine("{0,16}{1,10}{2,17}", value, 
                        BitConverter::IsLittleEndian ? "Little" : " Big", 
                        BitConverter::ToString(byteArray));
    }
}
// This example displays output like the following:
//              Integer    Endian       Byte Array
//                  ---    ------       ----------
//                    0    Little      00-00-00-00
//                   15    Little      0F-00-00-00
//                  -15    Little      F1-FF-FF-FF
//              1048576    Little      00-00-10-00
//             -1048576    Little      00-00-F0-FF
//           1000000000    Little      00-CA-9A-3B
//          -1000000000    Little      00-36-65-C4
//          -2147483648    Little      00-00-00-80
//           2147483647    Little      FF-FF-FF-7F
using System;

class Example
{
    public static void Main( )
    {
        // Define an array of integers.
        int[] values = { 0, 15, -15, 0x100000,  -0x100000, 1000000000,
                         -1000000000, int.MinValue, int.MaxValue };

        // Convert each integer to a byte array.
        Console.WriteLine("{0,16}{1,10}{2,17}", "Integer",
                          "Endian", "Byte Array");
        Console.WriteLine("{0,16}{1,10}{2,17}", "---", "------",
                          "----------" );
        foreach (var value in values) {
          byte[] byteArray = BitConverter.GetBytes(value);
          Console.WriteLine("{0,16}{1,10}{2,17}", value,
                            BitConverter.IsLittleEndian ? "Little" : " Big",
                            BitConverter.ToString(byteArray));
        }
    }
}
// This example displays output like the following:
//              Integer    Endian       Byte Array
//                  ---    ------       ----------
//                    0    Little      00-00-00-00
//                   15    Little      0F-00-00-00
//                  -15    Little      F1-FF-FF-FF
//              1048576    Little      00-00-10-00
//             -1048576    Little      00-00-F0-FF
//           1000000000    Little      00-CA-9A-3B
//          -1000000000    Little      00-36-65-C4
//          -2147483648    Little      00-00-00-80
//           2147483647    Little      FF-FF-FF-7F
open System

// Define a list of integers.
let values = 
    [ 0; 15; -15; 0x100000;  -0x100000; 1000000000
      -1000000000; Int32.MinValue; Int32.MaxValue ]

// Convert each integer to a byte array.
printfn "%16s%10s%17s" "Integer" "Endian" "Byte Array"
printfn "%16s%10s%17s" "---" "------" "----------"
for value in values do
    let byteArray = BitConverter.GetBytes value
    printfn $"""%16i{value}%10s{if BitConverter.IsLittleEndian then "Little" else " Big"}%17s{BitConverter.ToString byteArray}"""

// This example displays output like the following:
//              Integer    Endian       Byte Array
//                  ---    ------       ----------
//                    0    Little      00-00-00-00
//                   15    Little      0F-00-00-00
//                  -15    Little      F1-FF-FF-FF
//              1048576    Little      00-00-10-00
//             -1048576    Little      00-00-F0-FF
//           1000000000    Little      00-CA-9A-3B
//          -1000000000    Little      00-36-65-C4
//          -2147483648    Little      00-00-00-80
//           2147483647    Little      FF-FF-FF-7F
Module Example
    Public Sub Main( )
        ' Define an array of integers.
        Dim values() As Integer  = { 0, 15, -15, &h100000,  -&h100000, 1000000000,
                                     -1000000000, Int32.MinValue, Int32.MaxValue }
          
        ' Convert each integer to a byte array.
        Console.WriteLine("{0,16}{1,10}{2,17}", "Integer", 
                          "Endian", "Byte Array")
        Console.WriteLine("{0,16}{1,10}{2,17}", "---", "------", 
                          "----------" )
        For Each value In values
          Dim byteArray() As Byte = BitConverter.GetBytes(value)
          Console.WriteLine("{0,16}{1,10}{2,17}", value, 
                            If(BitConverter.IsLittleEndian, "Little", " Big"), 
                            BitConverter.ToString(byteArray))
        Next
    End Sub
End Module
' This example displays output like the following:
'              Integer    Endian       Byte Array
'                  ---    ------       ----------
'                    0    Little      00-00-00-00
'                   15    Little      0F-00-00-00
'                  -15    Little      F1-FF-FF-FF
'              1048576    Little      00-00-10-00
'             -1048576    Little      00-00-F0-FF
'           1000000000    Little      00-CA-9A-3B
'          -1000000000    Little      00-36-65-C4
'          -2147483648    Little      00-00-00-80
'           2147483647    Little      FF-FF-FF-7F

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(Int64)

以位元組陣列形式傳回指定的 64 位元帶正負號整數值。

public:
 static cli::array <System::Byte> ^ GetBytes(long value);
public static byte[] GetBytes (long value);
static member GetBytes : int64 -> byte[]
Public Shared Function GetBytes (value As Long) As Byte()

參數

value
Int64

要轉換的數字。

傳回

Byte[]

長度為 8 的位元組陣列。

範例

下列範例會呼叫 方法, GetBytes 將陣列 Byte 中的每個 Int64 專案轉換成陣列。

using namespace System;

void main()
{
    // Define an array of Int64 values.
    array<Int64>^ values = { 0, 0xFFFFFF, -0xFFFFFF, 1000000000, -1000000000,
                            0x100000000, -0x100000000, 0xAAAAAAAAAAAA, 
                            -0xAAAAAAAAAAAA, 1000000000000000000, 
                            -1000000000000000000, Int64::MinValue, 
                            Int64::MaxValue};
      
    Console::WriteLine( "{0,22}{1,10}{2,30}", "Int64", "Endian", "Byte Array");
    Console::WriteLine( "{0,22}{1,10}{2,30}", "----", "------", "----------");
      
    for each (Int64 value in values) {
        // Convert each Int64 value to a byte array.
        array<Byte>^ byteArray = BitConverter::GetBytes(value);
        // Display the result.
        Console::WriteLine("{0,22}{1,10}{2,30}", value, 
                           BitConverter::IsLittleEndian ? "Little" : " Big",
                           BitConverter::ToString(byteArray));
    }  
}
// The example displays output like the following:
//                      Int64    Endian                    Byte Array
//                       ----    ------                    ----------
//                          0    Little       00-00-00-00-00-00-00-00
//                   16777215    Little       FF-FF-FF-00-00-00-00-00
//                  -16777215    Little       01-00-00-FF-FF-FF-FF-FF
//                 1000000000    Little       00-CA-9A-3B-00-00-00-00
//                -1000000000    Little       00-36-65-C4-FF-FF-FF-FF
//                 4294967296    Little       00-00-00-00-01-00-00-00
//                -4294967296    Little       00-00-00-00-FF-FF-FF-FF
//            187649984473770    Little       AA-AA-AA-AA-AA-AA-00-00
//           -187649984473770    Little       56-55-55-55-55-55-FF-FF
//        1000000000000000000    Little       00-00-64-A7-B3-B6-E0-0D
//       -1000000000000000000    Little       00-00-9C-58-4C-49-1F-F2
//       -9223372036854775808    Little       00-00-00-00-00-00-00-80
//        9223372036854775807    Little       FF-FF-FF-FF-FF-FF-FF-7F
using System;

class Example
{
    public static void Main()
    {
        // Define an array of Int64 values.
        long[] values = { 0, 0xFFFFFF, -0xFFFFFF, 1000000000, -1000000000,
                          0x100000000, -0x100000000, 0xAAAAAAAAAAAA,
                          -0xAAAAAAAAAAAA, 1000000000000000000,
                          -1000000000000000000, long.MinValue,
                          long.MaxValue };

        Console.WriteLine( "{0,22}{1,10} {2,30}", "Int64", "Endian", "Byte Array");
        Console.WriteLine( "{0,22}{1,10} {2,30}", "----", "------", "----------" );

        foreach (var value in values) {
            // Convert each Int64 value to a byte array.
            byte[] byteArray = BitConverter.GetBytes(value);
            // Display the result.
            Console.WriteLine("{0,22}{1,10}{2,30}", value,
                              BitConverter.IsLittleEndian ? "Little" : "Big",
                              BitConverter.ToString(byteArray));
        }
    }
}
// The example displays output like the following:
//                    Int64    Endian                     Byte Array
//                     ----    ------                     ----------
//                        0    Little       00-00-00-00-00-00-00-00
//                 16777215    Little       FF-FF-FF-00-00-00-00-00
//                -16777215    Little       01-00-00-FF-FF-FF-FF-FF
//               1000000000    Little       00-CA-9A-3B-00-00-00-00
//              -1000000000    Little       00-36-65-C4-FF-FF-FF-FF
//               4294967296    Little       00-00-00-00-01-00-00-00
//              -4294967296    Little       00-00-00-00-FF-FF-FF-FF
//          187649984473770    Little       AA-AA-AA-AA-AA-AA-00-00
//         -187649984473770    Little       56-55-55-55-55-55-FF-FF
//      1000000000000000000    Little       00-00-64-A7-B3-B6-E0-0D
//     -1000000000000000000    Little       00-00-9C-58-4C-49-1F-F2
//     -9223372036854775808    Little       00-00-00-00-00-00-00-80
//      9223372036854775807    Little       FF-FF-FF-FF-FF-FF-FF-7F
open System

// Define a list of Int64 values.
let values = 
    [ 0L; 0xFFFFFFL; -0xFFFFFFL; 1000000000L; -1000000000L
      0x100000000L; -0x100000000L; 0xAAAAAAAAAAAAL
      -0xAAAAAAAAAAAAL; 1000000000000000000L
      -1000000000000000000L; Int64.MinValue; Int64.MaxValue ]

printfn "%22s%10s %30s" "Int64" "Endian" "Byte Array"
printfn "%22s%10s %30s" "----" "------" "----------"

for value in values do
    // Convert each Int64 value to a byte array.
    let byteArray = BitConverter.GetBytes value
    // Display the result.
    printfn $"""%22i{value}%10s{if BitConverter.IsLittleEndian then "Little" else "Big"} %30s{BitConverter.ToString byteArray}"""


// The example displays output like the following:
//                    Int64    Endian                     Byte Array
//                     ----    ------                     ----------
//                        0    Little       00-00-00-00-00-00-00-00
//                 16777215    Little       FF-FF-FF-00-00-00-00-00
//                -16777215    Little       01-00-00-FF-FF-FF-FF-FF
//               1000000000    Little       00-CA-9A-3B-00-00-00-00
//              -1000000000    Little       00-36-65-C4-FF-FF-FF-FF
//               4294967296    Little       00-00-00-00-01-00-00-00
//              -4294967296    Little       00-00-00-00-FF-FF-FF-FF
//          187649984473770    Little       AA-AA-AA-AA-AA-AA-00-00
//         -187649984473770    Little       56-55-55-55-55-55-FF-FF
//      1000000000000000000    Little       00-00-64-A7-B3-B6-E0-0D
//     -1000000000000000000    Little       00-00-9C-58-4C-49-1F-F2
//     -9223372036854775808    Little       00-00-00-00-00-00-00-80
//      9223372036854775807    Little       FF-FF-FF-FF-FF-FF-FF-7F
Public Module Example
    Public Sub Main()
        ' Define an array of Int64 values.
        Dim values() As Long = { 0, &hFFFFFF, -&hFFFFFF, 1000000000, -1000000000,
                                 &h100000000, -&h100000000, &hAAAAAAAAAAAA, 
                                 -&hAAAAAAAAAAAA, 1000000000000000000, 
                                 -1000000000000000000, Long.MinValue, 
                                 Long.MaxValue }
        
        Console.WriteLine( "{0,22}{1,10}{2,30}", "Int64", "Endian", "Byte Array" )
        Console.WriteLine( "{0,22}{1,10}{2,30}", "----", "------", "----------" )
        
        For Each value in values
            ' Convert each Int64 value to a byte array.
            Dim byteArray() As Byte = BitConverter.GetBytes(value)
            ' Display the result.
            Console.WriteLine("{0,22}{1,10}{2,30}", value, 
                              If(BitConverter.IsLittleEndian, "Little", "Big"),
                              BitConverter.ToString(byteArray))
        
        Next  
    End Sub
End Module
' The example displays the following output:
'                      Int64    Endian                    Byte Array
'                       ----    ------                    ----------
'                          0    Little       00-00-00-00-00-00-00-00
'                   16777215    Little       FF-FF-FF-00-00-00-00-00
'                  -16777215    Little       01-00-00-FF-FF-FF-FF-FF
'                 1000000000    Little       00-CA-9A-3B-00-00-00-00
'                -1000000000    Little       00-36-65-C4-FF-FF-FF-FF
'                 4294967296    Little       00-00-00-00-01-00-00-00
'                -4294967296    Little       00-00-00-00-FF-FF-FF-FF
'            187649984473770    Little       AA-AA-AA-AA-AA-AA-00-00
'           -187649984473770    Little       56-55-55-55-55-55-FF-FF
'        1000000000000000000    Little       00-00-64-A7-B3-B6-E0-0D
'       -1000000000000000000    Little       00-00-9C-58-4C-49-1F-F2
'       -9223372036854775808    Little       00-00-00-00-00-00-00-80
'        9223372036854775807    Little       FF-FF-FF-FF-FF-FF-FF-7F

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(Single)

傳回指定的單精確度浮點數值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(float value);
public static byte[] GetBytes (float value);
static member GetBytes : single -> byte[]
Public Shared Function GetBytes (value As Single) As Byte()

參數

value
Single

要轉換的數字。

傳回

Byte[]

長度為 4 的位元組陣列。

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 Single 轉換為 Byte 陣列。

// Example of the BitConverter::GetBytes( float ) method.
using namespace System;

// Convert a float argument to a byte array and display it.
void GetBytesSingle( float argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,16:E7}{1,20}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( float ) "
   "\nmethod generates the following output.\n" );
   Console::WriteLine( "{0,16:E7}{1,20}", "float", "byte array" );
   Console::WriteLine( "{0,16:E7}{1,20}", "-----", "----------" );
   
   // Convert float values and display the results.
   GetBytesSingle( 0.0F );
   GetBytesSingle( 1.0F );
   GetBytesSingle( 15.0F );
   GetBytesSingle( 65535.0F );
   GetBytesSingle( 0.00390625F );
   GetBytesSingle( 0.00000000023283064365386962890625F );
   GetBytesSingle( 1.2345E-35F );
   GetBytesSingle( 1.2345671F );
   GetBytesSingle( 1.2345673F );
   GetBytesSingle( 1.2345677F );
   GetBytesSingle( 1.23456789E+35F );
   GetBytesSingle( Single::MinValue );
   GetBytesSingle( Single::MaxValue );
   GetBytesSingle( Single::Epsilon );
   GetBytesSingle( Single::NaN );
   GetBytesSingle( Single::NegativeInfinity );
   GetBytesSingle( Single::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
*/
// Example of the BitConverter.GetBytes( float ) method.
using System;

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

    // Convert a float argument to a byte array and display it.
    public static void GetBytesSingle( float argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( float ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "float", "byte array" );
        Console.WriteLine( formatter, "-----", "----------" );

        // Convert float values and display the results.
        GetBytesSingle( 0.0F );
        GetBytesSingle( 1.0F );
        GetBytesSingle( 15.0F );
        GetBytesSingle( 65535.0F );
        GetBytesSingle( 0.00390625F );
        GetBytesSingle( 0.00000000023283064365386962890625F );
        GetBytesSingle( 1.2345E-35F );
        GetBytesSingle( 1.2345671F );
        GetBytesSingle( 1.2345673F );
        GetBytesSingle( 1.2345677F );
        GetBytesSingle( 1.23456789E+35F );
        GetBytesSingle( float.MinValue );
        GetBytesSingle( float.MaxValue );
        GetBytesSingle( float.Epsilon );
        GetBytesSingle( float.NaN );
        GetBytesSingle( float.NegativeInfinity );
        GetBytesSingle( 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
*/
open System

let print obj1 obj2 = printfn $"{obj1,16:E7}{obj2,20}"

// Convert a float argument to a byte array and display it.
let getBytesSingle (argument: float32) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(float32) \nmethod generates the following output.\n"
print "float32" "byte array"
print "-----" "----------"

// Convert float values and display the results.
getBytesSingle 0.0F
getBytesSingle 1.0F
getBytesSingle 15.0F
getBytesSingle 65535.0F
getBytesSingle 0.00390625F
getBytesSingle 0.00000000023283064365386962890625F
getBytesSingle 1.2345E-35F
getBytesSingle 1.2345671F
getBytesSingle 1.2345673F
getBytesSingle 1.2345677F
getBytesSingle 1.23456789E+35F
getBytesSingle Single.MinValue
getBytesSingle Single.MaxValue
getBytesSingle Single.Epsilon
getBytesSingle Single.NaN
getBytesSingle Single.NegativeInfinity
getBytesSingle Single.PositiveInfinity


// This example of the BitConverter.GetBytes(float32)
// method generates the following output.
//
//          float32          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( Single ) method.
Module GetBytesSingleDemo

    Const formatter As String = "{0,16:E7}{1,20}"
 
    ' Convert a Single argument to a Byte array and display it.
    Sub GetBytesSingle( argument As Single )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( Single ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Single", "Byte array" )
        Console.WriteLine( formatter, "------", "----------" )
          
        ' Convert Single values and display the results.
        GetBytesSingle( 0.0F )
        GetBytesSingle( 1.0F )
        GetBytesSingle( 15.0F )
        GetBytesSingle( 65535.0F )
        GetBytesSingle( 0.00390625F )
        GetBytesSingle( 0.00000000023283064365386962890625F )
        GetBytesSingle( 1.2345E-35F )
        GetBytesSingle( 1.2345671F )
        GetBytesSingle( 1.2345673F )
        GetBytesSingle( 1.2345677F )
        GetBytesSingle( 1.23456789E+35F )
        GetBytesSingle( Single.MinValue )
        GetBytesSingle( Single.MaxValue )
        GetBytesSingle( Single.Epsilon )
        GetBytesSingle( Single.NaN )
        GetBytesSingle( Single.NegativeInfinity )
        GetBytesSingle( 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

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(UInt16)

重要

此 API 不符合 CLS 規範。

傳回指定的 16 位元不帶正負號的整數值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(System::UInt16 value);
[System.CLSCompliant(false)]
public static byte[] GetBytes (ushort value);
[<System.CLSCompliant(false)>]
static member GetBytes : uint16 -> byte[]
Public Shared Function GetBytes (value As UShort) As Byte()

參數

value
UInt16

要轉換的數字。

傳回

Byte[]

長度為 2 的位元組陣列。

屬性

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 UInt16 轉換為 Byte 陣列。

// Example of the BitConverter::GetBytes( unsigned short ) method.
using namespace System;

// Convert an unsigned short argument to a byte array and display it.
void GetBytesUInt16( unsigned short argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,14}{1,13}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( unsigned "
   "short ) \nmethod generates the following output.\n" );
   Console::WriteLine( "{0,14}{1,13}", "unsigned short", "byte array" );
   Console::WriteLine( "{0,14}{1,13}", "--------------", "----------" );
   
   // Convert unsigned short values and display the results.
   GetBytesUInt16( 15 );
   GetBytesUInt16( 1023 );
   GetBytesUInt16( 10000 );
   GetBytesUInt16( UInt16::MinValue );
   GetBytesUInt16( Int16::MaxValue );
   GetBytesUInt16( UInt16::MaxValue );
}

/*
This example of the BitConverter::GetBytes( unsigned short )
method generates the following output.

unsigned short   byte array
--------------   ----------
            15        0F-00
          1023        FF-03
         10000        10-27
             0        00-00
         32767        FF-7F
         65535        FF-FF
*/
// Example of the BitConverter.GetBytes( ushort ) method.
using System;

class GetBytesUInt16Demo
{
    const string formatter = "{0,10}{1,13}";

    // Convert a ushort argument to a byte array and display it.
    public static void GetBytesUInt16( ushort argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( ushort ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "ushort", "byte array" );
        Console.WriteLine( formatter, "------", "----------" );

        // Convert ushort values and display the results.
        GetBytesUInt16( 15 );
        GetBytesUInt16( 1023 );
        GetBytesUInt16( 10000 );
        GetBytesUInt16( ushort.MinValue );
        GetBytesUInt16( (ushort)short.MaxValue );
        GetBytesUInt16( ushort.MaxValue );
    }
}

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

    ushort   byte array
    ------   ----------
        15        0F-00
      1023        FF-03
     10000        10-27
         0        00-00
     32767        FF-7F
     65535        FF-FF
*/
open System

let print obj1 obj2 = printfn $"{obj1,10}{obj2,13}"

// Convert a ushort argument to a byte array and display it.
let getBytesUInt16 (argument: uint16) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(uint16) \nmethod generates the following output.\n"
print "ushort" "byte array"
print "------" "----------"

// Convert ushort values and display the results.
getBytesUInt16 15us
getBytesUInt16 1023us
getBytesUInt16 10000us
getBytesUInt16 UInt16.MinValue
getBytesUInt16 (uint16 Int16.MaxValue)
getBytesUInt16 UInt16.MaxValue


// This example of the BitConverter.GetBytes(uint16)
// method generates the following output.
//
//     ushort   byte array
//     ------   ----------
//         15        0F-00
//       1023        FF-03
//      10000        10-27
//          0        00-00
//      32767        FF-7F
//      65535        FF-FF
' Example of the BitConverter.GetBytes( UInt16 ) method.
Module GetBytesUInt16Demo

    Const formatter As String = "{0,10}{1,13}"
 
    ' Convert a UInt16 argument to a Byte array and display it.
    Sub GetBytesUInt16( argument As UInt16 )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( UInt16 ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "UInt16", "Byte array" )
        Console.WriteLine( formatter, "------", "----------" )
          
        ' Convert UInt16 values and display the results.
        GetBytesUInt16( Convert.ToUInt16( 15 ) )
        GetBytesUInt16( Convert.ToUInt16( 1023 ) )
        GetBytesUInt16( Convert.ToUInt16( 10000 ) )
        GetBytesUInt16( Convert.ToUInt16( 0 ) )
        GetBytesUInt16( Convert.ToUInt16( Int16.MaxValue ) )
        GetBytesUInt16( Convert.ToUInt16( 65535 ) )
    End Sub 
End Module

' This example of the BitConverter.GetBytes( UInt16 )
' method generates the following output.
' 
'     UInt16   Byte array
'     ------   ----------
'         15        0F-00
'       1023        FF-03
'      10000        10-27
'          0        00-00
'      32767        FF-7F
'      65535        FF-FF

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(UInt32)

重要

此 API 不符合 CLS 規範。

傳回指定的 32 位元不帶正負號的整數值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(System::UInt32 value);
[System.CLSCompliant(false)]
public static byte[] GetBytes (uint value);
[<System.CLSCompliant(false)>]
static member GetBytes : uint32 -> byte[]
Public Shared Function GetBytes (value As UInteger) As Byte()

參數

value
UInt32

要轉換的數字。

傳回

Byte[]

長度為 4 的位元組陣列。

屬性

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 UInt32 轉換為 Byte 陣列。

// Example of the BitConverter::GetBytes( unsigned int ) method.
using namespace System;

// Convert an unsigned int argument to a byte array and display it.
void GetBytesUInt32( unsigned int argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,16}{1,20}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( unsigned "
   "int ) \nmethod generates the following output.\n" );
   Console::WriteLine( "{0,16}{1,20}", "unsigned int", "byte array" );
   Console::WriteLine( "{0,16}{1,20}", "------------", "----------" );
   
   // Convert unsigned int values and display the results.
   GetBytesUInt32( 15 );
   GetBytesUInt32( 1023 );
   GetBytesUInt32( 0x100000 );
   GetBytesUInt32( 1000000000 );
   GetBytesUInt32( UInt32::MinValue );
   GetBytesUInt32( Int32::MaxValue );
   GetBytesUInt32( UInt32::MaxValue );
}

/*
This example of the BitConverter::GetBytes( unsigned int )
method generates the following output.

    unsigned int          byte array
    ------------          ----------
              15         0F-00-00-00
            1023         FF-03-00-00
         1048576         00-00-10-00
      1000000000         00-CA-9A-3B
               0         00-00-00-00
      2147483647         FF-FF-FF-7F
      4294967295         FF-FF-FF-FF
*/
// Example of the BitConverter.GetBytes( uint ) method.
using System;

class GetBytesUInt32Demo
{
    const string formatter = "{0,16}{1,20}";

    // Convert a uint argument to a byte array and display it.
    public static void GetBytesUInt32( uint argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( uint ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "uint", "byte array" );
        Console.WriteLine( formatter, "----", "----------" );

        // Convert uint values and display the results.
        GetBytesUInt32( 15 );
        GetBytesUInt32( 1023 );
        GetBytesUInt32( 0x100000 );
        GetBytesUInt32( 1000000000 );
        GetBytesUInt32( uint.MinValue );
        GetBytesUInt32( int.MaxValue );
        GetBytesUInt32( uint.MaxValue );
    }
}

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

            uint          byte array
            ----          ----------
              15         0F-00-00-00
            1023         FF-03-00-00
         1048576         00-00-10-00
      1000000000         00-CA-9A-3B
               0         00-00-00-00
      2147483647         FF-FF-FF-7F
      4294967295         FF-FF-FF-FF
*/
open System

let print obj1 obj2 = printfn $"{obj1,16}{obj2,20}"

// Convert a uint argument to a byte array and display it.
let getBytesUInt32 (argument: uint) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(uint) \nmethod generates the following output.\n"
print "uint" "byte array"
print "----" "----------"

// Convert uint values and display the results.
getBytesUInt32 15u
getBytesUInt32 1023u
getBytesUInt32 0x100000u
getBytesUInt32 1000000000u
getBytesUInt32 UInt32.MinValue
getBytesUInt32 (uint Int32.MaxValue)
getBytesUInt32 UInt32.MaxValue


// This example of the BitConverter.GetBytes(uint)
// method generates the following output.
//
//             uint          byte array
//             ----          ----------
//               15         0F-00-00-00
//             1023         FF-03-00-00
//          1048576         00-00-10-00
//       1000000000         00-CA-9A-3B
//                0         00-00-00-00
//       2147483647         FF-FF-FF-7F
//       4294967295         FF-FF-FF-FF
' Example of the BitConverter.GetBytes( UInt32 ) method.
Module GetBytesUInt32Demo

    Const formatter As String = "{0,16}{1,20}"
 
    ' Convert a UInt32 argument to a Byte array and display it.
    Sub GetBytesUInt32( argument As UInt32 )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( UInt32 ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "UInt32", "Byte array" )
        Console.WriteLine( formatter, "------", "----------" )
          
        ' Convert UInt32 values and display the results.
        GetBytesUInt32( Convert.ToUInt32( 15 ) )
        GetBytesUInt32( Convert.ToUInt32( 1023 ) )
        GetBytesUInt32( Convert.ToUInt32( &H100000 ) )
        GetBytesUInt32( Convert.ToUInt32( 1000000000 ) )
        GetBytesUInt32( Convert.ToUInt32( 0 ) )
        GetBytesUInt32( Convert.ToUInt32( Int32.MaxValue ) )
        GetBytesUInt32( Convert.ToUInt32( 4294967295 ) )
    End Sub 
End Module

' This example of the BitConverter.GetBytes( UInt32 )
' method generates the following output.
' 
'           UInt32          Byte array
'           ------          ----------
'               15         0F-00-00-00
'             1023         FF-03-00-00
'          1048576         00-00-10-00
'       1000000000         00-CA-9A-3B
'                0         00-00-00-00
'       2147483647         FF-FF-FF-7F
'       4294967295         FF-FF-FF-FF

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於

GetBytes(UInt64)

重要

此 API 不符合 CLS 規範。

傳回指定的 64 位元不帶正負號的整數值為位元組陣列。

public:
 static cli::array <System::Byte> ^ GetBytes(System::UInt64 value);
[System.CLSCompliant(false)]
public static byte[] GetBytes (ulong value);
[<System.CLSCompliant(false)>]
static member GetBytes : uint64 -> byte[]
Public Shared Function GetBytes (value As ULong) As Byte()

參數

value
UInt64

要轉換的數字。

傳回

Byte[]

長度為 8 的位元組陣列。

屬性

範例

下列程式碼範例會使用 GetBytes 方法,將值的位模式 UInt64 轉換為 Byte 陣列。

// Example of the BitConverter::GetBytes( unsigned __int64 ) method.
using namespace System;

// Convert an unsigned __int64 argument to a byte array and display it.
void GetBytesUInt64( unsigned __int64 argument )
{
   array<Byte>^byteArray = BitConverter::GetBytes( argument );
   Console::WriteLine( "{0,22}{1,30}", argument, BitConverter::ToString( byteArray ) );
}

int main()
{
   Console::WriteLine( "This example of the BitConverter::GetBytes( unsigned "
   "__int64 ) \nmethod generates the following output.\n" );
   Console::WriteLine( "{0,22}{1,30}", "unsigned __int64", "byte array" );
   Console::WriteLine( "{0,22}{1,30}", "----------------", "----------" );
   
   // Convert unsigned __int64 values and display the results.
   GetBytesUInt64( 0xFFFFFF );
   GetBytesUInt64( 1000000000 );
   GetBytesUInt64( 0x100000000 );
   GetBytesUInt64( 0xAAAAAAAAAAAA );
   GetBytesUInt64( 1000000000000000000 );
   GetBytesUInt64( 10000000000000000000 );
   GetBytesUInt64( UInt64::MinValue );
   GetBytesUInt64( Int64::MaxValue );
   GetBytesUInt64( UInt64::MaxValue );
}

/*
This example of the BitConverter::GetBytes( unsigned __int64 )
method generates the following output.

      unsigned __int64                    byte array
      ----------------                    ----------
              16777215       FF-FF-FF-00-00-00-00-00
            1000000000       00-CA-9A-3B-00-00-00-00
            4294967296       00-00-00-00-01-00-00-00
       187649984473770       AA-AA-AA-AA-AA-AA-00-00
   1000000000000000000       00-00-64-A7-B3-B6-E0-0D
  10000000000000000000       00-00-E8-89-04-23-C7-8A
                     0       00-00-00-00-00-00-00-00
   9223372036854775807       FF-FF-FF-FF-FF-FF-FF-7F
  18446744073709551615       FF-FF-FF-FF-FF-FF-FF-FF
*/
// Example of the BitConverter.GetBytes( ulong ) method.
using System;

class GetBytesUInt64Demo
{
    const string formatter = "{0,22}{1,30}";

    // Convert a ulong argument to a byte array and display it.
    public static void GetBytesUInt64( ulong argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( ulong ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "ulong", "byte array" );
        Console.WriteLine( formatter, "-----", "----------" );

        // Convert ulong values and display the results.
        GetBytesUInt64( 0xFFFFFF );
        GetBytesUInt64( 1000000000 );
        GetBytesUInt64( 0x100000000 );
        GetBytesUInt64( 0xAAAAAAAAAAAA );
        GetBytesUInt64( 1000000000000000000 );
        GetBytesUInt64( 10000000000000000000 );
        GetBytesUInt64( ulong.MinValue );
        GetBytesUInt64( long.MaxValue );
        GetBytesUInt64( ulong.MaxValue );
    }
}

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

                 ulong                    byte array
                 -----                    ----------
              16777215       FF-FF-FF-00-00-00-00-00
            1000000000       00-CA-9A-3B-00-00-00-00
            4294967296       00-00-00-00-01-00-00-00
       187649984473770       AA-AA-AA-AA-AA-AA-00-00
   1000000000000000000       00-00-64-A7-B3-B6-E0-0D
  10000000000000000000       00-00-E8-89-04-23-C7-8A
                     0       00-00-00-00-00-00-00-00
   9223372036854775807       FF-FF-FF-FF-FF-FF-FF-7F
  18446744073709551615       FF-FF-FF-FF-FF-FF-FF-FF
*/
open System

let print obj1 obj2 = printfn $"{obj1,22}{obj2,30}";

// Convert a ulong argument to a byte array and display it.
let getBytesUInt64 (argument: uint64) =
    let byteArray = BitConverter.GetBytes argument
    
    BitConverter.ToString byteArray
    |> print argument

printfn "This example of the BitConverter.GetBytes(uint64) \nmethod generates the following output.\n"
print "ulong" "byte array"
print "-----" "----------"

// Convert ulong values and display the results.
getBytesUInt64 0xFFFFFFuL
getBytesUInt64 1000000000uL
getBytesUInt64 0x100000000uL
getBytesUInt64 0xAAAAAAAAAAAAuL
getBytesUInt64 1000000000000000000uL
getBytesUInt64 10000000000000000000uL
getBytesUInt64 UInt64.MinValue
getBytesUInt64 (uint64 Int64.MaxValue)
getBytesUInt64 UInt64.MaxValue


// This example of the BitConverter.GetBytes( ulong )
// method generates the following output.
//
//                  ulong                    byte array
//                  -----                    ----------
//               16777215       FF-FF-FF-00-00-00-00-00
//             1000000000       00-CA-9A-3B-00-00-00-00
//             4294967296       00-00-00-00-01-00-00-00
//        187649984473770       AA-AA-AA-AA-AA-AA-00-00
//    1000000000000000000       00-00-64-A7-B3-B6-E0-0D
//   10000000000000000000       00-00-E8-89-04-23-C7-8A
//                      0       00-00-00-00-00-00-00-00
//    9223372036854775807       FF-FF-FF-FF-FF-FF-FF-7F
//   18446744073709551615       FF-FF-FF-FF-FF-FF-FF-FF
' Example of the BitConverter.GetBytes( UInt64 ) method.
Imports System.Globalization

Module GetBytesUInt64Demo

    Const formatter As String = "{0,22}{1,30}"
 
    ' Convert a UInt64 argument to a Byte array and display it.
    Sub GetBytesUInt64( argument As UInt64 )

        Dim byteArray As Byte( ) = BitConverter.GetBytes( argument )
        Console.WriteLine( formatter, argument, _
            BitConverter.ToString( byteArray ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the BitConverter.GetBytes( UInt64 ) " & _
            vbCrLf & "method generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "UInt64", "Byte array" )
        Console.WriteLine( formatter, "------", "----------" )
          
        ' Convert UInt64 values and display the results.
        GetBytesUInt64( Convert.ToUInt64( &HFFFFFF ) )
        GetBytesUInt64( Convert.ToUInt64( 1000000000 ) )
        GetBytesUInt64( Convert.ToUInt64( &H100000000 ) )
        GetBytesUInt64( Convert.ToUInt64( &HAAAAAAAAAAAA ) )
        GetBytesUInt64( Convert.ToUInt64( 1000000000000000000 ) )
        GetBytesUInt64( UInt64.Parse( "10000000000000000000" ) )
        GetBytesUInt64( Convert.ToUInt64( 0 ) )
        GetBytesUInt64( Convert.ToUInt64( Int64.MaxValue ) )
        GetBytesUInt64( UInt64.Parse( "18446744073709551615" ) )
    End Sub 
End Module

' This example of the BitConverter.GetBytes( UInt64 )
' method generates the following output.
' 
'                 UInt64                    Byte array
'                 ------                    ----------
'               16777215       FF-FF-FF-00-00-00-00-00
'             1000000000       00-CA-9A-3B-00-00-00-00
'             4294967296       00-00-00-00-01-00-00-00
'        187649984473770       AA-AA-AA-AA-AA-AA-00-00
'    1000000000000000000       00-00-64-A7-B3-B6-E0-0D
'   10000000000000000000       00-00-E8-89-04-23-C7-8A
'                      0       00-00-00-00-00-00-00-00
'    9223372036854775807       FF-FF-FF-FF-FF-FF-FF-7F
'   18446744073709551615       FF-FF-FF-FF-FF-FF-FF-FF

備註

方法所 GetBytes 傳回陣列中的位元組順序取決於電腦架構是小到大或大端。

另請參閱

適用於