/// <summary> /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns> public static long ToInt64(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 8); uint h = ToUInt32(value, startIndex + 4); uint l = ToUInt32(value, startIndex); return((((long)h) << 32) | l); }
/// <summary> /// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns> public static short ToInt16(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 2); byte b1 = value[startIndex++]; byte b0 = value[startIndex]; return((short)((b0 << 8) | b1)); }
public static uint ToUInt32(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 4); return (value[startIndex] | (uint)value[startIndex + 1] << 8 | (uint)value[startIndex + 2] << 16 | (uint)value[startIndex + 3] << 24); }
public static uint ToUInt32(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 4); uint x = ToUInt16(value, startIndex); x <<= 16; x |= ToUInt16(value, startIndex + 2); return(x); }
/// <summary> /// Returns a decimal number converted from sixteen bytes at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A decimal number formed by sixteen bytes beginning at <paramref name="startIndex"/>.</returns> public static decimal ToDecimal(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 16); var bits = new int[4]; for (int i = 0; i < bits.Length; ++i) { bits[i] = ToInt32(value, startIndex + i * sizeof(int)); } return(new decimal(bits)); }
/// <summary> /// Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A double-precision floating point number formed by eight bytes beginning at <paramref name="startIndex"/>.</returns> public static double ToDouble(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 8); return(BitConverter.Int64BitsToDouble(ToInt64(value, startIndex))); }
/// <summary> /// Returns a single-precision floating point number converted from four bytes at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A single-precision floating point number formed by four bytes beginning at <paramref name="startIndex"/>.</returns> public static float ToSingle(byte[] value, int startIndex) { BitConverterServices.ValidateToArguments(value, startIndex, 4); return(BitConverterEx.Int32BitsToSingle(ToInt32(value, startIndex))); }