BigInteger.ToByteArray Method

Converts a BigInteger value to a byte array.

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

member ToByteArray : unit -> byte[] 

Return Value

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

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(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.

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

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft