From f073c13f0372088353b22b729fb54c27006b1667 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 7 Mar 2021 11:56:52 +0000 Subject: [PATCH] (#7) (#15) IEnumerable -> IROList - GetString renamed to ToString - No longer has optional Encoding parameter --- X10D/src/ByteExtensions.cs | 116 -------- .../ListExtensions/ListOfByteExtensions.cs | 252 ++++++++++++++++++ 2 files changed, 252 insertions(+), 116 deletions(-) delete mode 100644 X10D/src/ByteExtensions.cs create mode 100644 X10D/src/ListExtensions/ListOfByteExtensions.cs diff --git a/X10D/src/ByteExtensions.cs b/X10D/src/ByteExtensions.cs deleted file mode 100644 index 2baa8db..0000000 --- a/X10D/src/ByteExtensions.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace X10D -{ - /// - /// Extension methods for . - /// - public static class ByteExtensions - { - /// - /// Gets a literally representing the raw values in the []. - /// - /// The bytes to get. - /// Returns a . - public static string AsString(this IEnumerable bytes) - { - return BitConverter.ToString(bytes.ToArray()); - } - - /// - /// Converts the [] to an . - /// - /// The bytes to convert. - /// Returns an . - public static short GetInt16(this IEnumerable bytes) - { - return BitConverter.ToInt16(bytes.ToArray(), 0); - } - - /// - /// Converts the [] to an . - /// - /// The bytes to convert. - /// Returns an . - public static int GetInt32(this IEnumerable bytes) - { - return BitConverter.ToInt32(bytes.ToArray(), 0); - } - - /// - /// Converts the [] to an . - /// - /// The bytes to convert. - /// Returns an . - public static long GetInt64(this IEnumerable bytes) - { - return BitConverter.ToInt64(bytes.ToArray(), 0); - } - - /// - /// Gets a representing the value the [] with - /// encoding. - /// - /// The bytes to convert. - /// Returns a . - public static string GetString(this IEnumerable bytes) - { - return bytes.GetString(Encoding.UTF8); - } - - /// - /// Gets a representing the value the [] with the provided encoding. - /// - /// The bytes to convert. - /// The encoding to use. - /// Returns a . - /// is . - public static string GetString(this IEnumerable bytes, Encoding encoding) - { - if (encoding is null) - { - throw new ArgumentNullException(nameof(encoding)); - } - - // ReSharper disable once SuggestVarOrType_Elsewhere - var array = bytes.ToArray(); - return encoding.GetString(array, 0, array.Length); - } - - /// - /// Converts the [] to a . - /// - /// The bytes to convert. - /// Returns an . - [CLSCompliant(false)] - public static ushort GetUInt16(this IEnumerable bytes) - { - return BitConverter.ToUInt16(bytes.ToArray(), 0); - } - - /// - /// Converts the [] to an . - /// - /// The bytes to convert. - /// Returns an . - [CLSCompliant(false)] - public static uint GetUInt32(this IEnumerable bytes) - { - return BitConverter.ToUInt32(bytes.ToArray(), 0); - } - - /// - /// Converts the [] to an . - /// - /// The bytes to convert. - /// Returns an . - [CLSCompliant(false)] - public static ulong GetUInt64(this IEnumerable bytes) - { - return BitConverter.ToUInt64(bytes.ToArray(), 0); - } - } -} diff --git a/X10D/src/ListExtensions/ListOfByteExtensions.cs b/X10D/src/ListExtensions/ListOfByteExtensions.cs new file mode 100644 index 0000000..4f5f474 --- /dev/null +++ b/X10D/src/ListExtensions/ListOfByteExtensions.cs @@ -0,0 +1,252 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace X10D.ListExtensions +{ + /// + /// Extension methods for array. + /// + public static class ListOfByteExtensions + { + /// + /// Converts the numeric value of each element of a specified list of bytes to its equivalent hexadecimal string + /// representation. + /// + /// The source list of bytes. + /// + /// A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in + /// ; for example, "7F-2C-4A-00". + /// + /// is . + public static string AsString(this IReadOnlyList source) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToString(source.ToArray()); + } + + /// + /// Returns a double-precision floating point number converted from eight bytes. + /// + /// The source list of bytes. + /// A double-precision floating point number formed by eight bytes. + /// is . + public static double ToDouble(this IReadOnlyList source) + { + return ToDouble(source, 0); + } + + /// + /// Returns a double-precision floating point number converted from eight bytes at a specified position in a list of + /// bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// + /// A double-precision floating point number formed by eight bytes beginning at . + /// + /// is . + public static double ToDouble(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToDouble(source.ToArray(), startIndex); + } + + /// + /// Returns a 16-bit signed integer converted from two bytes. + /// + /// The source list of bytes. + /// A 16-bit signed integer formed by two bytes. + /// is . + public static short ToInt16(this IReadOnlyList source) + { + return ToInt16(source, 0); + } + + /// + /// Returns a 16-bit signed integer converted from two bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 16-bit signed integer formed by two bytes beginning at . + /// is . + public static short ToInt16(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToInt16(source.ToArray(), startIndex); + } + + /// + /// Returns a 32-bit signed integer converted from four bytes. + /// + /// The source list of bytes. + /// A 32-bit signed integer formed by four bytes. + /// is . + public static int ToInt32(this IReadOnlyList source) + { + return ToInt32(source, 0); + } + + /// + /// Returns a 32-bit signed integer converted from four bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 32-bit signed integer formed by four bytes beginning at . + /// is . + public static int ToInt32(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToInt32(source.ToArray(), startIndex); + } + + /// + /// Returns a 64-bit signed integer converted from eight bytes. + /// + /// The source list of bytes. + /// A 64-bit signed integer formed by eight bytes. + /// is . + public static long ToInt64(this IReadOnlyList source) + { + return ToInt64(source, 0); + } + + /// + /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 64-bit signed integer formed by eight bytes beginning at . + /// is . + public static long ToInt64(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToInt64(source.ToArray(), startIndex); + } + + /// + /// Returns a single-precision floating point number converted from four bytes. + /// + /// The source list of bytes. + /// A single-precision floating point number formed by four bytes. + /// is . + public static float ToSingle(this IReadOnlyList source) + { + return ToSingle(source, 0); + } + + /// + /// Returns a single-precision floating point number converted from four bytes at a specified position in a list of + /// bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// + /// A single-precision floating point number formed by four bytes beginning at . + /// + /// is . + public static float ToSingle(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToSingle(source.ToArray(), startIndex); + } + + /// + /// Decodes all the bytes within the current list of bytes to a string, using a specified encoding. + /// + /// The source list of bytes. + /// The encoding which should be used to decode . + /// A string that contains the results of decoding the specified sequence of bytes. + /// + /// is . + /// -or- + /// is . + /// + public static string ToString(this IReadOnlyList source, Encoding encoding) + { + Validate.IsNotNull(source, nameof(source)); + Validate.IsNotNull(encoding, nameof(encoding)); + + return encoding.GetString(source.ToArray()); + } + + /// + /// Returns a 16-bit unsigned integer converted from two bytes. + /// + /// The source list of bytes. + /// A 16-bit unsigned integer formed by two bytes. + /// is . + [CLSCompliant(false)] + public static ushort ToUInt16(this IReadOnlyList source) + { + return ToUInt16(source, 0); + } + + /// + /// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 16-bit unsigned integer formed by two bytes beginning at . + /// is . + [CLSCompliant(false)] + public static ushort ToUInt16(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToUInt16(source.ToArray(), startIndex); + } + + /// + /// Returns a 32-bit unsigned integer converted from four bytes. + /// + /// The source list of bytes. + /// A 32-bit unsigned integer formed by four bytes. + /// is . + [CLSCompliant(false)] + public static uint ToUInt32(this IReadOnlyList source) + { + return ToUInt32(source, 0); + } + + /// + /// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 32-bit unsigned integer formed by four bytes beginning at . + /// is . + [CLSCompliant(false)] + public static uint ToUInt32(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToUInt32(source.ToArray(), startIndex); + } + + /// + /// Returns a 64-bit unsigned integer converted from eight bytes. + /// + /// The source list of bytes. + /// A 64-bit unsigned integer formed by eight bytes. + /// is . + [CLSCompliant(false)] + public static ulong ToUInt64(this IReadOnlyList source) + { + return ToUInt64(source, 0); + } + + /// + /// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a list of bytes. + /// + /// The source list of bytes. + /// The starting position within . + /// A 64-bit unsigned integer formed by eight bytes beginning at . + /// is . + [CLSCompliant(false)] + public static ulong ToUInt64(this IReadOnlyList source, int startIndex) + { + Validate.IsNotNull(source, nameof(source)); + return BitConverter.ToUInt64(source.ToArray(), startIndex); + } + } +}