// Converts an array of bytes into an int.
unsafe public static int ToInt32(byte[] value, int startIndex)
{
if (value == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if ((uint)startIndex >= value.Length) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
if (startIndex > value.Length - 4) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
fixed (byte* pbyte = &value[startIndex]) {
if (startIndex % 4 == 0) {
// data is aligned
return *((int*)pbyte);
}
else {
if (IsLittleEndian) {
return (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24);
}
else {
return (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
}
}
}
}
ICAgICAgICAvLyBDb252ZXJ0cyBhbiBhcnJheSBvZiBieXRlcyBpbnRvIGFuIGludC4KICAgICAgICB1bnNhZmUgcHVibGljIHN0YXRpYyBpbnQgVG9JbnQzMihieXRlW10gdmFsdWUsIGludCBzdGFydEluZGV4KQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHZhbHVlID09IG51bGwpIHsKICAgICAgICAgICAgICAgIFRocm93SGVscGVyLlRocm93QXJndW1lbnROdWxsRXhjZXB0aW9uKEV4Y2VwdGlvbkFyZ3VtZW50LnZhbHVlKTsKICAgICAgICAgICAgfQogICAgICAgICAgIAogICAgICAgICAgICBpZiAoKHVpbnQpc3RhcnRJbmRleCA+PSB2YWx1ZS5MZW5ndGgpIHsKICAgICAgICAgICAgICAgIFRocm93SGVscGVyLlRocm93QXJndW1lbnRPdXRPZlJhbmdlRXhjZXB0aW9uKEV4Y2VwdGlvbkFyZ3VtZW50LnN0YXJ0SW5kZXgsIEV4Y2VwdGlvblJlc291cmNlLkFyZ3VtZW50T3V0T2ZSYW5nZV9JbmRleCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAKICAgICAgICAgICAgaWYgKHN0YXJ0SW5kZXggPiB2YWx1ZS5MZW5ndGggLSA0KSB7CiAgICAgICAgICAgICAgICBUaHJvd0hlbHBlci5UaHJvd0FyZ3VtZW50RXhjZXB0aW9uKEV4Y2VwdGlvblJlc291cmNlLkFyZ19BcnJheVBsdXNPZmZUb29TbWFsbCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAKICAgICAgICAgICAgZml4ZWQgKGJ5dGUqIHBieXRlID0gJnZhbHVlW3N0YXJ0SW5kZXhdKSB7CiAgICAgICAgICAgICAgICBpZiAoc3RhcnRJbmRleCAlIDQgPT0gMCkgewogICAgICAgICAgICAgICAgICAgIC8vIGRhdGEgaXMgYWxpZ25lZAogICAgICAgICAgICAgICAgICAgIHJldHVybiAqKChpbnQqKXBieXRlKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICAgIGlmIChJc0xpdHRsZUVuZGlhbikgewogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gKCpwYnl0ZSkgfCAoKihwYnl0ZSArIDEpIDw8IDgpIHwgKCoocGJ5dGUgKyAyKSA8PCAxNikgfCAoKihwYnl0ZSArIDMpIDw8IDI0KTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAoKnBieXRlIDw8IDI0KSB8ICgqKHBieXRlICsgMSkgPDwgMTYpIHwgKCoocGJ5dGUgKyAyKSA8PCA4KSB8ICgqKHBieXRlICsgMykpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0=