BigInteger.ToByteArray BigInteger.ToByteArray BigInteger.ToByteArray BigInteger.ToByteArray Method

定义

重载

ToByteArray() ToByteArray() ToByteArray() ToByteArray()

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

ToByteArray(Boolean, Boolean) ToByteArray(Boolean, Boolean) 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() ToByteArray() ToByteArray() 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[]

转换为字节数组的当前 BigInteger 对象的值。The value of the current BigInteger object converted to an array of bytes.

示例

下面的示例演示如何在BigInteger字节数组中表示某些值。The following example illustrates how some BigInteger values are represented in byte arrays.

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

注解

此方法返回的数组中的单个字节以小字节序顺序显示。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. 数组的第一个字节反映BigInteger值的前八位, 第二个字节反映后八位, 依此类推。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. 例如, 值1024或0x0400 存储为以下两个字节的数组:For example, the value 1024, or 0x0400, is stored as the following array of two bytes:

元素Element 字节值Byte value
00 0x000x00
11 0x040x04

负值将使用最简洁形式的2的补码表示形式写入数组。Negative values are written to the array using two's complement representation in the most compact form possible. 例如,-1 表示为单个字节0xFF , 其值为0xFF 0xFF 0xFF 0xFF 0xFF, 而不是具有多个元素的数组, 例如、、、和。 0xFFFor 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.

由于两个补数表示法始终解释数组中最后一个字节 (位置Array.Length - 1处的字节) 的最高序位, 因此该方法返回一个字节数组, 该数组包含一个值为零的附加元素消除可解释为设置其符号位的正值。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. 例如, 值120或0x78表示为单字节数组:。 0x78For example, the value 120 or 0x78 is represented as a single-byte array: 0x78. 但是, 128 或0x80, 表示为一个2字节的数组: 0x800x00However, 128, or 0x80, is represented as a two-byte array: 0x80, 0x00.

可以通过将BigInteger值存储到字节数组中来往返值, 然后BigInteger(Byte[])使用构造函数还原该值。You can round-trip a BigInteger value by storing it to a byte array and then restoring it using the BigInteger(Byte[]) constructor.

注意

如果代码在恢复值之前修改此方法返回的数组中的单个字节的值, 则必须确保不会意外更改符号位。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.

ToByteArray(Boolean, Boolean) ToByteArray(Boolean, Boolean) 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.

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 Boolean Boolean Boolean

如果使用无符号编码,则为 true;否则为 falsetrue to use unsigned encoding; otherwise, false.

isBigEndian
Boolean Boolean Boolean Boolean

如果以大端字节顺序写入字节,则为 true;否则为 falsetrue to write the bytes in a big-endian byte order; otherwise, false.

返回

Byte[]

转换为字节数组的当前 BigInteger 对象的值。The value of the current BigInteger object converted to an array of bytes.

异常

如果 isUnsignedtrueSign 为负。If isUnsigned is true and Sign is negative.

注解

可在四33022个不同的数组中导出整数值:The integer value 33022 can be exported in four different arrays:

属性Properties 结果Result
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 }

适用于