# BigInteger.ToByteArray 方法

## 重载

 ToByteArray() 将 BigInteger 值转换为字节数组。Converts a BigInteger value to a byte array. ToByteArray(Boolean, Boolean) 使用尽可能少的字节数返回此 BigInteger 的值作为字节数组。Returns the value of this BigInteger as a byte array using the fewest number of bytes possible. 如果值为零，则返回一个字节（其元素为 0x00）的数组。If the value is zero, returns an array of one byte whose element is 0x00.

## ToByteArray()

BigInteger 值转换为字节数组。Converts a BigInteger value to a byte array.

``````public:
cli::array <System::Byte> ^ ToByteArray();``````
``public byte[] ToByteArray ();``
``member this.ToByteArray : unit -> byte[]``
``Public Function ToByteArray () As Byte()``

Byte[]

### 示例

``````using System;
using System.Numerics;

public class Example
{
static byte[] bytes;

public static void Main()
{
BigInteger[] numbers = { BigInteger.MinusOne, BigInteger.One,
BigInteger.Zero, 120, 128, 255, 1024,
Int64.MinValue, Int64.MaxValue,
BigInteger.Parse("90123123981293054321") };
foreach (BigInteger number in numbers)
{
bytes = number.ToByteArray();
Console.Write("{0} ({1}) -> ", number, number.ToString(GetSpecifier()));
Console.Write("{0} bytes: ", bytes.Length);
foreach (byte byteValue in bytes)
Console.Write("{0:X2} ", byteValue);

Console.WriteLine();
}
}

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
``````
``````Imports System.Numerics

Module Example
Dim bytes() As Byte

Public Sub Main()
Dim numbers() As BigInteger = { BigInteger.MinusOne, BigInteger.One,
BigInteger.Zero, 120, 128, 255, 1024,
Int64.MinValue, Int64.MaxValue,
BigInteger.Parse("90123123981293054321") }
For Each number As BigInteger In numbers
bytes = number.ToByteArray()
Console.Write("{0} ({1}) -> ", number, number.ToString(GetSpecifier()))
Console.Write("{0} bytes: ", bytes.Length)
For Each byteValue As Byte In bytes
Console.Write("{0:X2} ", byteValue)
Next
Console.WriteLine()
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
``````

00 0x000x00
11 0x040x04

## ToByteArray(Boolean, Boolean)

``public byte[] ToByteArray (bool isUnsigned = false, bool isBigEndian = false);``
``member this.ToByteArray : bool * bool -> byte[]``
``Public Function ToByteArray (Optional isUnsigned As Boolean = false, Optional isBigEndian As Boolean = false) As Byte()``

isUnsigned
Boolean

isBigEndian
Boolean

Byte[]

### 注解

`isUnsigned: false, isBigEndian: false` `new byte[] { 0xFE, 0x80, 0x00 }`
`isUnsigned: false, isBigEndian: true` `new byte[] { 0x00, 0x80, 0xFE }`
`isUnsigned: true, isBigEndian: false` `new byte[] { 0xFE, 0x80 }`
`isUnsigned: true, isBigEndian: true` `new byte[] { 0x80, 0xFE }`