/// <summary> /// Fills the array with eight bytes of the specified 64-bit signed integer value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">The number to convert.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(long value, byte[] buffer, int startIndex) { BitConverterServices.ValidateFillArguments(buffer, startIndex, 8); FillBytes((int)value, buffer, startIndex); FillBytes((int)(value >> 32), buffer, startIndex + 4); }
/// <summary> /// Fills the array with two bytes of the specified 16-bit signed integer value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">The number to convert.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(short value, byte[] buffer, int startIndex) { BitConverterServices.ValidateFillArguments(buffer, startIndex, 2); buffer[startIndex++] = (byte)value; buffer[startIndex] = (byte)(value >> 8); }
/// <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> /// Fills the array with sixteen bytes of the specified decimal value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">The number to convert.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(decimal value, byte[] buffer, int startIndex) { BitConverterServices.ValidateFillArguments(buffer, startIndex, 16); var bits = decimal.GetBits(value); for (int i = 0; i < bits.Length; ++i) { FillBytes(bits[i], buffer, startIndex + i * sizeof(int)); } }
/// <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 <see cref="Boolean"/> value converted from one byte 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><c>true</c> if the byte at startIndex in value is nonzero; otherwise, <c>false</c>.</returns> public static bool ToBoolean(byte[] value, int startIndex) => BitConverterServices.ToBoolean(value, startIndex);
/// <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))); }
/// <summary> /// Fills the array with one byte of the specified <see cref="bool"/> value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">A <see cref="bool"/> value.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(bool value, byte[] buffer, int startIndex) => BitConverterServices.FillBytes(value, buffer, startIndex);
/// <summary> /// Fills the array with eight bytes of the specified double-precision floating point value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">The number to convert.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(double value, byte[] buffer, int startIndex) { BitConverterServices.ValidateFillArguments(buffer, startIndex, 8); FillBytes(BitConverter.DoubleToInt64Bits(value), buffer, startIndex); }
/// <summary> /// Fills the array with four bytes of the specified single-precision floating point value beginning at <paramref name="startIndex"/>. /// </summary> /// <param name="value">The number to convert.</param> /// <param name="buffer">The array of bytes to store converted value at.</param> /// <param name="startIndex">The start index where converted value is to be stored at <paramref name="buffer"/>.</param> public static void FillBytes(float value, byte[] buffer, int startIndex) { BitConverterServices.ValidateFillArguments(buffer, startIndex, 4); FillBytes(BitConverterEx.SingleToInt32Bits(value), buffer, startIndex); }