BigInteger.ToByteArray 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| ToByteArray() | 
						 将 BigInteger 值转换为字节数组。  | 
        	
| ToByteArray(Boolean, Boolean) | 
						 使用尽可能少的字节数返回此 BigInteger 的值作为字节数组。 如果值为零,则返回一个字节(其元素为 0x00)的数组。  | 
        	
ToByteArray()
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
将 BigInteger 值转换为字节数组。
public:
 cli::array <System::Byte> ^ ToByteArray();
	public byte[] ToByteArray();
	member this.ToByteArray : unit -> byte[]
	Public Function ToByteArray () As Byte()
	返回
转换为字节数组的当前 BigInteger 对象的值。
示例
以下示例演示了某些 BigInteger 值在字节数组中的表示方式。
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
open System
open System.Numerics
let numbers =
    [| BigInteger.MinusOne
       BigInteger.One
       BigInteger.Zero
       120
       128
       255
       1024
       Int64.MinValue
       Int64.MaxValue
       BigInteger.Parse("90123123981293054321") |]
for number in numbers do
    let bytes = number.ToByteArray()
    printf $"""{number} ({number.ToString("X" + (bytes.Length * 2).ToString())}) -> """
    printf $"{bytes.Length} bytes: "
    for byteValue in bytes do
        printf $"{byteValue:X2} "
    printfn ""
// 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
    	注解
此方法返回的数组中的各个字节按 little-endian 顺序显示。 也就是说,该值的低位字节位于高位字节之前。 数组的第一个字节反映值的前 8 位 BigInteger ,第二个字节反映接下来的 8 位,依此。 例如,值 1024 或 0x0400 存储为以下两个字节的数组:
| 元素 | 字节值 | 
|---|---|
| 0 | 0x00 | 
| 1 | 0x04 | 
负值以最紧凑的形式使用二的补数表示形式写入数组。 例如,-1 表示为单个字节,其值为 0xFF ,而不是表示为具有多个元素(如 0xFF、 或 0xFF0xFF、0xFF、0xFF、)0xFF的数组。
由于二者的补码表示形式始终将数组中最后一个字节的最高位 (位置) 的字节 Array.Length- 1 解释为符号位,因此该方法返回一个字节数组,其中包含一个值为零的额外元素,以消除正值的歧义,否则这些正值可能被解释为设置其符号位。 例如,值 120 或 0x78 表示为单字节数组: 0x78。 但是,128 或 0x80表示为双字节数组: 0x80、 0x00。
可以通过将值存储到字节数组,然后使用 构造函数还原该值来BigInteger(Byte[])往返BigInteger。
注意
如果代码在还原该值之前修改此方法返回的数组中单个字节的值,则必须确保不会无意中更改符号位。 例如,如果修改增加了一个正值,以便设置字节数组的最后一个元素中的最高顺序位,则可以在数组的末尾添加一个值为零的新字节。
适用于
ToByteArray(Boolean, Boolean)
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
- Source:
 - BigInteger.cs
 
使用尽可能少的字节数返回此 BigInteger 的值作为字节数组。 如果值为零,则返回一个字节(其元素为 0x00)的数组。
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
 
如果使用无符号编码,则为 true;否则为 false。
- isBigEndian
 - Boolean
 
如果以大端字节顺序写入字节,则为 true;否则为 false。
返回
转换为字节数组的当前 BigInteger 对象的值。
例外
如果 isUnsigned 为 true 且 Sign 为负。
注解
可以在四个不同的数组中导出整数值 33022 :
| 属性 | 结果 | 
|---|---|
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 } |