Share via


BitConverter.GetBytes Method (UInt64)

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

Returns the specified 64-bit unsigned integer value as an array of bytes.

This API is not CLS-compliant. 

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

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function GetBytes ( _
    value As ULong _
) As Byte()
[CLSCompliantAttribute(false)]
public static byte[] GetBytes(
    ulong 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 UInt64 values to Byte arrays with the GetBytes method.

' Example of the BitConverter.GetBytes( UInt64 ) method.
Imports System.Globalization

Module Example

   Const formatter As String = "{0,22}{1,30}"

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

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

      ' Convert UInt64 values and display the results.
      GetBytesUInt64(outputBlock, Convert.ToUInt64(&HFFFFFF))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(1000000000))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(&H100000000))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(&HAAAAAAAAAAAA))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(1000000000000000000))
      GetBytesUInt64(outputBlock, UInt64.Parse("10000000000000000000"))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(0))
      GetBytesUInt64(outputBlock, Convert.ToUInt64(Int64.MaxValue))
      GetBytesUInt64(outputBlock, 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
// Example of the BitConverter.GetBytes( ulong ) method.
using System;

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

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

      // Convert ulong values and display the results.
      GetBytesUInt64(outputBlock, 0xFFFFFF);
      GetBytesUInt64(outputBlock, 1000000000);
      GetBytesUInt64(outputBlock, 0x100000000);
      GetBytesUInt64(outputBlock, 0xAAAAAAAAAAAA);
      GetBytesUInt64(outputBlock, 1000000000000000000);
      GetBytesUInt64(outputBlock, 10000000000000000000);
      GetBytesUInt64(outputBlock, ulong.MinValue);
      GetBytesUInt64(outputBlock, long.MaxValue);
      GetBytesUInt64(outputBlock, 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
*/

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.