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);
}