Microsoft.Scripting.Math.BigInteger.Create C# (CSharp) Method

Create() public static method

Create a BigInteger from a little-endian twos-complement byte array (inverse of ToByteArray())
public static Create ( byte v ) : BigInteger
v byte
return BigInteger
    public static BigInteger Create(byte[] v) {
      //Contract.RequiresNotNull(v, "v");
      if (v.Length == 0) return Create(0);

      int byteCount = v.Length;
      int unalignedBytes = byteCount % 4;
      int dwordCount = byteCount / 4 + (unalignedBytes == 0 ? 0 : 1);
      uint[] data = new uint[dwordCount];

      bool isNegative = (v[byteCount - 1] & 0x80) == 0x80;

      bool isZero = true;

      // Copy all dwords, except but don't do the last one if it's not a full four bytes
      int curDword, curByte, byteInDword;
      curByte = 3;
      for (curDword = 0; curDword < dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++) {
        byteInDword = 0;
        while (byteInDword < 4) {
          if (v[curByte] != 0x00) isZero = false;
          data[curDword] <<= 8;
          data[curDword] |= v[curByte];
          curByte--;
          byteInDword++;
        }
        curByte += 8;
      }

      // Copy the last dword specially if it's not aligned
      if (unalignedBytes != 0) {
        if (isNegative) data[dwordCount - 1] = 0xffffffff;
        for (curByte = byteCount - 1; curByte >= byteCount - unalignedBytes; curByte--) {
          if (v[curByte] != 0x00) isZero = false;
          data[curDword] <<= 8;
          data[curDword] |= v[curByte];
        }
      }

      if (isZero) return Zero;

      if (isNegative) {
        makeTwosComplement(data);
        return new BigInteger(-1, data);
      }
      return new BigInteger(1, data);
    }

Same methods

BigInteger::Create ( decimal v ) : BigInteger
BigInteger::Create ( double v ) : BigInteger
BigInteger::Create ( int v ) : BigInteger
BigInteger::Create ( long v ) : BigInteger
BigInteger::Create ( uint v ) : BigInteger
BigInteger::Create ( ulong v ) : BigInteger