G
Guest
Now, if we take a look at the reflected BitConverter.ToInt32 method it looks
something like this:
public static unsafe int ToInt32(byte[] value, int startIndex)
{
if (value == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if (((ulong) startIndex) >= value.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex,
ExceptionResource.ArgumentOutOfRange_Index);
}
if (startIndex > (value.Length - 4))
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
fixed (byte* numRef1 = &(value[startIndex]))
{
if ((startIndex % 4) == 0)
{
return *(((int*) numRef1));
}
if (BitConverter.IsLittleEndian)
{
return (((numRef1[0] | (numRef1[1] << 8)) | (numRef1[2] <<
0x10)) | (numRef1[3] << 0x18));
}
return ((((numRef1[0] << 0x18) | (numRef1[1] << 0x10)) |
(numRef1[2] << 8)) | numRef1[3]);
}
}
What I don't understand is this:
if ((startIndex % 4) == 0)
{
return *(((int*) numRef1));
}
What prevents me from using this method when the remaining product isn't 0?
We already checked if there is 4 bytes to read so I should be able to.
Cheers
Morten
something like this:
public static unsafe int ToInt32(byte[] value, int startIndex)
{
if (value == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if (((ulong) startIndex) >= value.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex,
ExceptionResource.ArgumentOutOfRange_Index);
}
if (startIndex > (value.Length - 4))
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
fixed (byte* numRef1 = &(value[startIndex]))
{
if ((startIndex % 4) == 0)
{
return *(((int*) numRef1));
}
if (BitConverter.IsLittleEndian)
{
return (((numRef1[0] | (numRef1[1] << 8)) | (numRef1[2] <<
0x10)) | (numRef1[3] << 0x18));
}
return ((((numRef1[0] << 0x18) | (numRef1[1] << 0x10)) |
(numRef1[2] << 8)) | numRef1[3]);
}
}
What I don't understand is this:
if ((startIndex % 4) == 0)
{
return *(((int*) numRef1));
}
What prevents me from using this method when the remaining product isn't 0?
We already checked if there is 4 bytes to read so I should be able to.
Cheers
Morten