namespace System
{
- // The BitConverter class contains methods for
- // converting an array of bytes to one of the base data
- // types, as well as for converting a base data type to an
- // array of bytes.
+ /// <summary>
+ /// Converts base data types to an array of bytes, and an array of bytes to base data types.
+ /// </summary>
public static class BitConverter
{
// This field indicates the "endianess" of the architecture.
public static readonly bool IsLittleEndian = true;
#endif
- // Converts a Boolean into an array of bytes with length one.
+ /// <summary>
+ /// Returns the specified Boolean value as a byte array.
+ /// </summary>
+ /// <param name="value">A Boolean value.</param>
+ /// <returns>A byte array with length 1.</returns>
public static byte[] GetBytes(bool value)
{
byte[] r = new byte[1];
return r;
}
- // Converts a Boolean into a Span of bytes with length one.
+ /// <summary>
+ /// Converts a Boolean into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted Boolean.</param>
+ /// <param name="value">The Boolean to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, bool value)
{
if (destination.Length < sizeof(byte))
return true;
}
- // Converts a char into an array of bytes with length two.
+ /// <summary>
+ /// Returns the specified Unicode character value as a byte array.
+ /// </summary>
+ /// <param name="value">A Char value.</param>
+ /// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(char value)
{
byte[] bytes = new byte[sizeof(char)];
return bytes;
}
- // Converts a char into a Span
+ /// <summary>
+ /// Converts a character into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted character.</param>
+ /// <param name="value">The character to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, char value)
{
if (destination.Length < sizeof(char))
return true;
}
- // Converts a short into an array of bytes with length
- // two.
+ /// <summary>
+ /// Returns the specified 16-bit signed integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(short value)
{
byte[] bytes = new byte[sizeof(short)];
return bytes;
}
- // Converts a short into a Span
+ /// <summary>
+ /// Converts a 16-bit signed integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 16-bit signed integer.</param>
+ /// <param name="value">The 16-bit signed integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, short value)
{
if (destination.Length < sizeof(short))
return true;
}
- // Converts an int into an array of bytes with length
- // four.
+ /// <summary>
+ /// Returns the specified 32-bit signed integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte[sizeof(int)];
return bytes;
}
- // Converts an int into a Span
+ /// <summary>
+ /// Converts a 32-bit signed integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 32-bit signed integer.</param>
+ /// <param name="value">The 32-bit signed integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, int value)
{
if (destination.Length < sizeof(int))
return true;
}
- // Converts a long into an array of bytes with length
- // eight.
+ /// <summary>
+ /// Returns the specified 64-bit signed integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(long value)
{
byte[] bytes = new byte[sizeof(long)];
return bytes;
}
- // Converts a long into a Span
+ /// <summary>
+ /// Converts a 64-bit signed integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 64-bit signed integer.</param>
+ /// <param name="value">The 64-bit signed integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, long value)
{
if (destination.Length < sizeof(long))
return true;
}
- // Converts an ushort into an array of bytes with
- // length two.
+ /// <summary>
+ /// Returns the specified 16-bit unsigned integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 2.</returns>
[CLSCompliant(false)]
public static byte[] GetBytes(ushort value)
{
return bytes;
}
- // Converts a ushort into a Span
+ /// <summary>
+ /// Converts a 16-bit unsigned integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 16-bit unsigned integer.</param>
+ /// <param name="value">The 16-bit unsigned integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
[CLSCompliant(false)]
public static bool TryWriteBytes(Span<byte> destination, ushort value)
{
return true;
}
- // Converts an uint into an array of bytes with
- // length four.
+ /// <summary>
+ /// Returns the specified 32-bit unsigned integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 4.</returns>
[CLSCompliant(false)]
public static byte[] GetBytes(uint value)
{
return bytes;
}
- // Converts a uint into a Span
+ /// <summary>
+ /// Converts a 32-bit unsigned integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 32-bit unsigned integer.</param>
+ /// <param name="value">The 32-bit unsigned integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
[CLSCompliant(false)]
public static bool TryWriteBytes(Span<byte> destination, uint value)
{
return true;
}
- // Converts an unsigned long into an array of bytes with
- // length eight.
+ /// <summary>
+ /// Returns the specified 64-bit signed integer value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 8.</returns>
[CLSCompliant(false)]
public static byte[] GetBytes(ulong value)
{
return bytes;
}
- // Converts a ulong into a Span
+ /// <summary>
+ /// Converts a 64-bit unsigned integer into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted 64-bit unsigned integer.</param>
+ /// <param name="value">The 64-bit unsigned integer to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
[CLSCompliant(false)]
public static bool TryWriteBytes(Span<byte> destination, ulong value)
{
return true;
}
- // Converts a float into an array of bytes with length
- // four.
+ /// <summary>
+ /// Returns the specified half-precision floating point value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 2.</returns>
+ public static unsafe byte[] GetBytes(Half value)
+ {
+ byte[] bytes = new byte[sizeof(Half)];
+ Unsafe.As<byte, Half>(ref bytes[0]) = value;
+ return bytes;
+ }
+
+ /// <summary>
+ /// Converts a half-precision floating-point value into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted half-precision floating-point value.</param>
+ /// <param name="value">The half-precision floating-point value to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
+ public static unsafe bool TryWriteBytes(Span<byte> destination, Half value)
+ {
+ if (destination.Length < sizeof(Half))
+ return false;
+
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
+ return true;
+ }
+
+ /// <summary>
+ /// Returns the specified single-precision floating point value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(float value)
{
byte[] bytes = new byte[sizeof(float)];
return bytes;
}
- // Converts a float into a Span
+ /// <summary>
+ /// Converts a single-precision floating-point value into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted single-precision floating-point value.</param>
+ /// <param name="value">The single-precision floating-point value to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, float value)
{
if (destination.Length < sizeof(float))
return true;
}
- // Converts a double into an array of bytes with length
- // eight.
+ /// <summary>
+ /// Returns the specified double-precision floating point value as an array of bytes.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(double value)
{
byte[] bytes = new byte[sizeof(double)];
return bytes;
}
- // Converts a double into a Span
+ /// <summary>
+ /// Converts a double-precision floating-point value into a span of bytes.
+ /// </summary>
+ /// <param name="destination">When this method returns, the bytes representing the converted double-precision floating-point value.</param>
+ /// <param name="value">The double-precision floating-point value to convert.</param>
+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
public static bool TryWriteBytes(Span<byte> destination, double value)
{
if (destination.Length < sizeof(double))
return true;
}
- // Converts an array of bytes into a char.
+ /// <summary>
+ /// Returns a Unicode character converted from two bytes at a specified position in a byte array.
+ /// </summary>
+ /// <param name="value">An array.</param>
+ /// <param name="startIndex">The starting position within <paramref name="value"/>.</param>
+ /// <returns>A character formed by two bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException"><paramref name="startIndex"/> equals the length of <paramref name="value"/> minus 1.</exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static char ToChar(byte[] value, int startIndex) => unchecked((char)ToInt16(value, startIndex));
- // Converts a Span into a char
+ /// <summary>
+ /// Converts a read-only byte span into a character.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A character representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than the length of a <see cref="char"/>.</exception>
public static char ToChar(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(char))
return Unsafe.ReadUnaligned<char>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into a short.
+ /// <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 <paramref name="value"/>.</param>
+ /// <returns>A 16-bit signed integer formed by two bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException"><paramref name="startIndex"/> equals the length of <paramref name="value"/> minus 1.</exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static short ToInt16(byte[] value, int startIndex)
{
if (value == null)
return Unsafe.ReadUnaligned<short>(ref value[startIndex]);
}
- // Converts a Span into a short
+ /// <summary>
+ /// Converts a read-only byte span into a 16-bit signed integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 16-bit signed integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 2.</exception>
public static short ToInt16(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(short))
return Unsafe.ReadUnaligned<short>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into an int.
+ /// <summary>
+ /// Returns a 32-bit signed integer 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 <paramref name="value"/>.</param>
+ /// <returns>A 32-bit signed integer formed by four bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 3,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static int ToInt32(byte[] value, int startIndex)
{
if (value == null)
return Unsafe.ReadUnaligned<int>(ref value[startIndex]);
}
- // Converts a Span into an int
+ /// <summary>
+ /// Converts a read-only byte span into a 32-bit signed integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 32-bit signed integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 4.</exception>
public static int ToInt32(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(int))
return Unsafe.ReadUnaligned<int>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into a long.
+ /// <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 <paramref name="value"/>.</param>
+ /// <returns>A 64-bit signed integer formed by eight bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 7,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static long ToInt64(byte[] value, int startIndex)
{
if (value == null)
return Unsafe.ReadUnaligned<long>(ref value[startIndex]);
}
- // Converts a Span into a long
+ /// <summary>
+ /// Converts a read-only byte span into a 64-bit signed integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 64-bit signed integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 8.</exception>
public static long ToInt64(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(long))
return Unsafe.ReadUnaligned<long>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into an ushort.
- //
+ /// <summary>
+ /// Returns a 16-bit unsigned 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 <paramref name="value"/>.</param>
+ /// <returns>A 16-bit unsigned integer formed by two bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException"><paramref name="startIndex"/> equals the length of <paramref name="value"/> minus 1.</exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
[CLSCompliant(false)]
public static ushort ToUInt16(byte[] value, int startIndex) => unchecked((ushort)ToInt16(value, startIndex));
- // Converts a Span into a ushort
+ /// <summary>
+ /// Converts a read-only byte span into a 16-bit unsigned integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 16-bit unsigned integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 2.</exception>
[CLSCompliant(false)]
public static ushort ToUInt16(ReadOnlySpan<byte> value)
{
return Unsafe.ReadUnaligned<ushort>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into an uint.
- //
+ /// <summary>
+ /// Returns a 32-bit unsigned integer 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 <paramref name="value"/>.</param>
+ /// <returns>A 32-bit unsigned integer formed by four bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 3,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
[CLSCompliant(false)]
public static uint ToUInt32(byte[] value, int startIndex) => unchecked((uint)ToInt32(value, startIndex));
- // Convert a Span into a uint
+ /// <summary>
+ /// Converts a read-only byte span into a 32-bit unsigned integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 32-bit unsigned integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 4.</exception>
[CLSCompliant(false)]
public static uint ToUInt32(ReadOnlySpan<byte> value)
{
return Unsafe.ReadUnaligned<uint>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into an unsigned long.
- //
+ /// <summary>
+ /// Returns a 64-bit unsigned integer 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 <paramref name="value"/>.</param>
+ /// <returns>A 64-bit unsigned integer formed by eight bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 7,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
[CLSCompliant(false)]
public static ulong ToUInt64(byte[] value, int startIndex) => unchecked((ulong)ToInt64(value, startIndex));
- // Converts a Span into an unsigned long
+ /// <summary>
+ /// Converts a read-only byte span into a 64-bit unsigned integer.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A 64-bit unsigned integer representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 8.</exception>
[CLSCompliant(false)]
public static ulong ToUInt64(ReadOnlySpan<byte> value)
{
return Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into a float.
+ /// <summary>
+ /// Returns a half-precision floating point number 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 <paramref name="value"/>.</param>
+ /// <returns>A half-precision floating point number signed integer formed by two bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException"><paramref name="startIndex"/> equals the length of <paramref name="value"/> minus 1.</exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
+ public static Half ToHalf(byte[] value, int startIndex) => Int16BitsToHalf(ToInt16(value, startIndex));
+
+ /// <summary>
+ /// Converts a read-only byte span into a half-precision floating-point value.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A half-precision floating-point value representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 2.</exception>
+ public static unsafe Half ToHalf(ReadOnlySpan<byte> value)
+ {
+ if (value.Length < sizeof(Half))
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value);
+ return Unsafe.ReadUnaligned<Half>(ref MemoryMarshal.GetReference(value));
+ }
+
+ /// <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 <paramref name="value"/>.</param>
+ /// <returns>A single-precision floating point number formed by four bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 3,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static float ToSingle(byte[] value, int startIndex) => Int32BitsToSingle(ToInt32(value, startIndex));
- // Converts a Span into a float
+ /// <summary>
+ /// Converts a read-only byte span into a single-precision floating-point value.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A single-precision floating-point value representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 4.</exception>
public static float ToSingle(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(float))
return Unsafe.ReadUnaligned<float>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into a double.
+ /// <summary>
+ /// Returns a double-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 <paramref name="value"/>.</param>
+ /// <returns>A double-precision floating point number formed by eight bytes beginning at <paramref name="startIndex"/>.</returns>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="startIndex"/> is greater than or equal to the length of <paramref name="value"/> minus 7,
+ /// and is less than or equal to the length of <paramref name="value"/> minus 1.
+ /// </exception>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static double ToDouble(byte[] value, int startIndex) => Int64BitsToDouble(ToInt64(value, startIndex));
- // Converts a Span into a double
+ /// <summary>
+ /// Converts a read-only byte span into a double-precision floating-point value.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A double-precision floating-point value representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 8.</exception>
public static double ToDouble(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(double))
return Unsafe.ReadUnaligned<double>(ref MemoryMarshal.GetReference(value));
}
- // Converts an array of bytes into a String.
+ /// <summary>
+ /// Converts the numeric value of each element of a specified array of bytes
+ /// to its equivalent hexadecimal string representation.
+ /// </summary>
+ /// <param name="value">An array of bytes.</param>
+ /// <param name="startIndex">The starting position within <paramref name="value"/>.</param>
+ /// <param name="length">The number of array elements in <paramref name="value"/> to convert.</param>
+ /// <returns>A string of hexadecimal pairs separated by hyphens,
+ /// where each pair represents the corresponding element in a subarray of <paramref name="value"/>;
+ /// for example, "7F-2C-4A-00".</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
+ /// <exception cref="ArgumentOutOfRangeException">
+ /// <paramref name="startIndex"/> or <paramref name="length"/> is less than zero.
+ /// <para>-or-</para>
+ /// <paramref name="startIndex"/> is greater than zero and is greater than or equal to the length of <paramref name="value"/>.
+ /// </exception>
+ /// <exception cref="ArgumentException">
+ /// The combination of <paramref name="startIndex"/> and <paramref name="length"/> does not specify a position within <paramref name="value"/>;
+ /// that is, the <paramref name="startIndex"/> parameter is greater than the length of <paramref name="value"/> minus the <paramref name="length"/> parameter.
+ /// </exception>
public static string ToString(byte[] value, int startIndex, int length)
{
if (value == null)
});
}
- // Converts an array of bytes into a String.
+ /// <summary>
+ /// Converts the numeric value of each element of a specified array of bytes
+ /// to its equivalent hexadecimal string representation.
+ /// </summary>
+ /// <param name="value">An array of bytes.</param>
+ /// <returns>A string of hexadecimal pairs separated by hyphens,
+ /// where each pair represents the corresponding element in <paramref name="value"/>;
+ /// for example, "7F-2C-4A-00".</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
public static string ToString(byte[] value)
{
if (value == null)
return ToString(value, 0, value.Length);
}
- // Converts an array of bytes into a String.
+ /// <summary>
+ /// Converts the numeric value of each element of a specified array of bytes
+ /// to its equivalent hexadecimal string representation.
+ /// </summary>
+ /// <param name="value">An array of bytes.</param>
+ /// <param name="startIndex">The starting position within <paramref name="value"/>.</param>
+ /// <returns>A string of hexadecimal pairs separated by hyphens,
+ /// where each pair represents the corresponding element in a subarray of <paramref name="value"/>;
+ /// for example, "7F-2C-4A-00".</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static string ToString(byte[] value, int startIndex)
{
if (value == null)
return ToString(value, startIndex, value.Length - startIndex);
}
- /*==================================ToBoolean===================================
- **Action: Convert an array of bytes to a boolean value. We treat this array
- ** as if the first 4 bytes were an Int4 an operate on this value.
- **Returns: True if the Int4 value of the first 4 bytes is non-zero.
- **Arguments: value -- The byte array
- ** startIndex -- The position within the array.
- **Exceptions: See ToInt4.
- ==============================================================================*/
- // Converts an array of bytes into a boolean.
+ /// <summary>
+ /// Returns a Boolean value converted from two bytes at a specified position in a byte array.
+ /// </summary>
+ /// <param name="value">A byte array.</param>
+ /// <param name="startIndex">The index of the byte within <paramref name="value"/>.</param>
+ /// <returns><see langword="true"/> if the byte at <paramref name="startIndex"/> is nonzero; otherwise <see langword="false"/>.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
public static bool ToBoolean(byte[] value, int startIndex)
{
if (value == null)
return value[startIndex] != 0;
}
+ /// <summary>
+ /// Converts a read-only byte span into a Boolean value.
+ /// </summary>
+ /// <param name="value">A read-only span containing the bytes to convert.</param>
+ /// <returns>A Boolean representing the converted bytes.</returns>
+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 1.</exception>
public static bool ToBoolean(ReadOnlySpan<byte> value)
{
if (value.Length < sizeof(byte))
return Unsafe.ReadUnaligned<byte>(ref MemoryMarshal.GetReference(value)) != 0;
}
+ /// <summary>
+ /// Converts the specified double-precision floating point number to a 64-bit signed integer.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A 64-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe long DoubleToInt64Bits(double value)
{
return *((long*)&value);
}
+ /// <summary>
+ /// Converts the specified 64-bit signed integer to a double-precision floating point number.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A double-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe double Int64BitsToDouble(long value)
{
return *((double*)&value);
}
+ /// <summary>
+ /// Converts the specified single-precision floating point number to a 32-bit signed integer.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A 32-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SingleToInt32Bits(float value)
{
return *((int*)&value);
}
+ /// <summary>
+ /// Converts the specified 32-bit signed integer to a single-precision floating point number.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A single-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Int32BitsToSingle(int value)
{
return *((float*)&value);
}
+ /// <summary>
+ /// Converts the specified half-precision floating point number to a 16-bit signed integer.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A 16-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe short HalfToInt16Bits(Half value)
{
return *((short*)&value);
}
+ /// <summary>
+ /// Converts the specified 16-bit signed integer to a half-precision floating point number.
+ /// </summary>
+ /// <param name="value">The number to convert.</param>
+ /// <returns>A half-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Half Int16BitsToHalf(short value)
{