ToByteArray Method

# BigInteger.ToByteArray Method ()

.NET Framework (current version)

Converts a BigInteger value to a byte array.

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

## Syntax

```member ToByteArray : unit -> byte[]
```

#### Return Value

Type:

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(Byte[]) constructor.

Caution

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.

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

## Version Information

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone
Available since 8.1