BigInteger.ToByteArray 메서드

정의

오버로드

ToByteArray()

BigInteger 값을 바이트 배열로 변환합니다.

ToByteArray(Boolean, Boolean)

가능한 최소 바이트 수를 사용하여 이 BigInteger의 값을 바이트 배열로 반환합니다. 값이 0이면 해당 요소가0x00인 1바이트 배열을 반환합니다.

ToByteArray()

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

반환

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
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 순서로 표시됩니다. 즉, 값의 하위 바이트가 더 높은 바이트보다 우선합니다. 배열의 첫 번째 바이트는 값의 BigInteger 처음 8비트를 반영하고 두 번째 바이트는 다음 8비트를 반영합니다. 예를 들어 1024 또는 0x0400 값은 다음 두 바이트 배열로 저장됩니다.

요소 바이트 값
0 0x00
1 0x04

음수 값은 가능한 가장 압축된 형태로 두 개의 보수 표현을 사용하여 배열에 기록됩니다. 예를 들어 -1은 값 0xFF 이 , 또는 0xFF, 0xFF0xFF0xFF0xFF와 같은 여러 요소가 있는 배열이 아닌 단일 바이트로 0xFF표시됩니다.

두 개의 보수 표현은 항상 배열에서 마지막 바이트의 가장 높은 비트(위치 Array.Length- 1의 바이트)를 부호 비트로 해석하기 때문에 메서드는 부호 비트가 설정된 것으로 해석될 수 있는 양수 값을 구분하기 위해 값이 0인 추가 요소가 있는 바이트 배열을 반환합니다. 예를 들어 값 120 또는 0x78 는 단일 바이트 배열 0x78로 표시됩니다. 그러나 128 또는 0x80는 , 0x00의 2 바이트 배열0x80로 표시됩니다.

바이트 배열에 BigInteger 저장한 다음 생성자를 사용하여 BigInteger(Byte[]) 복원하여 값을 왕복할 수 있습니다.

주의

코드가 값을 복원하기 전에 이 메서드에서 반환된 배열의 개별 바이트 값을 수정하는 경우 부호 비트를 의도치 않게 변경하지 않도록 해야 합니다. 예를 들어 바이트 배열의 마지막 요소에서 가장 높은 순서의 비트가 설정되도록 수정하면 값이 0인 새 바이트를 배열 끝에 추가할 수 있습니다.

적용 대상

ToByteArray(Boolean, Boolean)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

가능한 최소 바이트 수를 사용하여 이 BigInteger의 값을 바이트 배열로 반환합니다. 값이 0이면 해당 요소가0x00인 1바이트 배열을 반환합니다.

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

Big endian 바이트 순서로 바이트를 쓰려면 true, 그렇지 않으면 false입니다.

반환

Byte[]

현재 BigInteger 개체를 바이트 배열로 변환한 값입니다.

예외

isUnsignedtrue이고 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 }

적용 대상