BigInteger.ToByteArray Method

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

Converts a BigInteger value to a byte array.

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

Syntax

'Declaration
Public Function ToByteArray As Byte()
public byte[] ToByteArray()

Return Value

Type: array<System.Byte[]
The value of the current BigInteger object converted to an array of bytes.

Remarks

The individual bytes in the array returned by this method appear in little-endian order. That is, the lower-order bytes of the value precede the higher-order bytes. The first byte of the array reflects the first eight bits of the BigInteger value, the second byte reflects the next eight bits, and so on. For example, the value 1024, or 0x0400, is stored as the following array of two bytes:

Element

Byte value

0

0x00

1

0x04

Negative values are written to the array using two's complement representation in the most compact form possible. For example, -1 is represented as a single byte whose value is 0xFF instead of as an array with multiple elements, such as 0xFF, 0xFF or 0xFF, 0xFF, 0xFF, 0xFF.

Because two's complement representation always interprets the highest-order bit of the last byte in the array (the byte at position Array.Length- 1) as the sign bit, the method returns a byte array with an extra element whose value is zero to disambiguate positive values that could otherwise be interpreted as having their sign bits set. For example, the value 120 or 0x78 is represented as a single-byte array: 0x78. However, 128, or 0x80, is represented as a two-byte array: 0x80, 0x00.

You can round-trip a BigInteger value by storing it to a byte array and then restoring it using the BigInteger(array<Byte[]) constructor.

Caution noteCaution:

 If your code modifies the value of individual bytes in the array returned by this method before it restores the value, you must make sure that you do not unintentionally change the sign bit. For example, if your modifications increase a positive value so that the highest-order bit in the last element of the byte array becomes set, you can add a new byte whose value is zero to the end of the array.

Examples

The following example illustrates how some BigInteger values are represented in byte arrays.

Imports System.Numerics

Module Example
   Dim bytes() As Byte

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim numbers() As BigInteger = { BigInteger.MinusOne, BigInteger.One, 
                                      BigInteger.Zero, 120, 128, 255, 1024, 
                                      Int64.MinValue, Int64.MaxValue, 
                                      BigInteger.Multiply(UInt64.MaxValue, 4) +  16336147686454847861ul }
      For Each number As BigInteger In numbers
         bytes = number.ToByteArray()
         outputBlock.Text += String.Format("{0} ({1}) -> ", number, number.ToString(GetSpecifier()))
         outputBlock.Text += String.Format("{0} bytes: ", bytes.Length)
         For Each byteValue As Byte In bytes
            outputBlock.Text &= String.Format("{0:X2} ", byteValue)
         Next
         outputBlock.Text &= vbCrLf
      Next
   End Sub

   Private Function GetSpecifier() As String
      Return "X" + CStr(bytes.Length * 2)
   End Function
End Module
' The example displays the following output:
'    -1 (FF) -> 1 bytes: FF
'    1 (01) -> 1 bytes: 01
'    0 (00) -> 1 bytes: 00
'    120 (78) -> 1 bytes: 78
'    128 (0080) -> 2 bytes: 80 00
'    255 (00FF) -> 2 bytes: FF 00
'    1024 (0400) -> 2 bytes: 00 04
'    -9223372036854775808 (8000000000000000) -> 8 bytes: 00 00 00 00 00 00 00 80
'    9223372036854775807 (7FFFFFFFFFFFFFFF) -> 8 bytes: FF FF FF FF FF FF FF 7F
'    90123123981293054321 (04E2B5A7C4A975E971) -> 9 bytes: 71 E9 75 A9 C4 A7 B5 E2 04
using System;
using System.Numerics;

public class Example
{
   static byte[] bytes;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      BigInteger[] numbers = { BigInteger.MinusOne, BigInteger.One, 
                               BigInteger.Zero, 120, 128, 255, 1024, 
                               Int64.MinValue, Int64.MaxValue, 
                               ((BigInteger)UInt64.MaxValue) * 4 +  16336147686454847861 };
      foreach (BigInteger number in numbers)
      {
         bytes = number.ToByteArray();
         outputBlock.Text += String.Format("{0} ({1}) -> ", number, number.ToString(GetSpecifier()));
         outputBlock.Text += String.Format("{0} bytes: ", bytes.Length);
         foreach (byte byteValue in bytes)
            outputBlock.Text += String.Format("{0:X2} ", byteValue);

         outputBlock.Text += "\n";
      }
   }

   private static string GetSpecifier()
   {
      return "X" + (bytes.Length * 2).ToString();
   }
}
// The example displays the following output:
//    -1 (FF) -> 1 bytes: FF
//    1 (01) -> 1 bytes: 01
//    0 (00) -> 1 bytes: 00
//    120 (78) -> 1 bytes: 78
//    128 (0080) -> 2 bytes: 80 00
//    255 (00FF) -> 2 bytes: FF 00
//    1024 (0400) -> 2 bytes: 00 04
//    -9223372036854775808 (8000000000000000) -> 8 bytes: 00 00 00 00 00 00 00 80
//    9223372036854775807 (7FFFFFFFFFFFFFFF) -> 8 bytes: FF FF FF FF FF FF FF 7F
//    90123123981293054321 (04E2B5A7C4A975E971) -> 9 bytes: 71 E9 75 A9 C4 A7 B5 E2 04

Version Information

Silverlight

Supported in: 5, 4

Platforms

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