예제 #1
0
        /// <summary>
        /// Reverses the endian of a primitive value such as int, short, float, double etc. (Not including structs).
        /// </summary>
        /// <param name="type">The individual value to be byte reversed.</param>
        /// <param name="swapped">The output variable to receive the swapped out value.</param>
        public static void Reverse <T>(ref T type, out T swapped) where T : unmanaged
        {
            // Declare an array for storing the data.
            byte[] data = Struct.GetBytes(ref type);
            Array.Reverse(data);

            // Use this base object for the storage of the value we are retrieving.
            Struct.FromArray <T>(data, out swapped);
        }
예제 #2
0
        /// <summary>
        /// Converts a span to a specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="data">A byte array containing data from which to extract a structure from.</param>
        /// <param name="length">The amount of elements to read from the span.</param>
        public static void FromArray <T>(Span <byte> data, out T[] value, int length = 0) where T : unmanaged
        {
            int structSize     = Struct.GetSize <T>();
            int structureCount = (length == 0) ? (data.Length) / structSize : length;

            value = new T[structureCount];

            for (int x = 0; x < value.Length; x++)
            {
                Struct.FromArray <T>(data, out value[x]);
                data = data.Slice(structSize);
            }
        }
예제 #3
0
        /// <summary>
        /// Converts a byte array to a specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="data">A byte array containing data from which to extract a structure from.</param>
        /// <param name="startIndex">The index in the byte array to read the element(s) from.</param>
        /// <param name="marshalElement">Set to true to marshal the element.</param>
        /// <param name="length">The amount of elements to read from the byte array.</param>
        public static void FromArray <T>(byte[] data, out T[] value, int startIndex = 0, bool marshalElement = false, int length = 0)
        {
            int structSize     = Struct.GetSize <T>(marshalElement);
            int structureCount = (length == 0) ? (data.Length - startIndex) / structSize : length;

            value = new T[structureCount];

            for (int x = 0; x < value.Length; x++)
            {
                int offset = startIndex + (structSize * x);
                Struct.FromArray <T>(data, out T result, offset, marshalElement);
                value[x] = result;
            }
        }
예제 #4
0
        /// <summary>
        /// Converts a byte array to a specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="data">A byte array containing data from which to extract a structure from.</param>
        /// <param name="startIndex">The index in the byte array to read the element(s) from.</param>
        /// <param name="length">The amount of elements to read from the byte array.</param>
        public static void FromArray <T>(byte[] data, out T[] value, int startIndex = 0, int length = 0) where T : unmanaged
        {
            int structSize     = Struct.GetSize <T>();
            int structureCount = (length == 0) ? (data.Length - startIndex) / structSize : length;

            value = new T[structureCount];

            for (int x = 0; x < value.Length; x++)
            {
                int offset = startIndex + (structSize * x);
                Struct.FromArray <T>(data, out T result, offset);
                value[x] = result;
            }
        }
예제 #5
0
        /// <summary>
        /// Converts a span to a specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="data">A byte array containing data from which to extract a structure from.</param>
        /// <param name="length">The amount of elements to read from the span.</param>
        public static void FromArray <T>(Span <byte> data, out T[] value, int length = 0) where T : unmanaged
        {
            int structSize     = Struct.GetSize <T>();
            int structureCount = (length == 0) ? (data.Length) / structSize : length;

#if NET5_0_OR_GREATER
            value = GC.AllocateUninitializedArray <T>(structureCount, false);
#else
            value = new T[structureCount];
#endif

            for (int x = 0; x < value.Length; x++)
            {
                Struct.FromArray <T>(data, out value[x]);
                data = data.Slice(structSize);
            }
        }
예제 #6
0
        /// <summary>
        /// Converts a byte array to a specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="data">A byte array containing data from which to extract a structure from.</param>
        /// <param name="startIndex">The index in the byte array to read the element(s) from.</param>
        /// <param name="length">The amount of elements to read from the byte array.</param>
        public static void FromArray <T>(byte[] data, out T[] value, int startIndex = 0, int length = 0) where T : unmanaged
        {
            int structSize     = Struct.GetSize <T>();
            int structureCount = (length == 0) ? (data.Length - startIndex) / structSize : length;

#if NET5_0_OR_GREATER
            value = GC.AllocateUninitializedArray <T>(structureCount, false);
#else
            value = new T[structureCount];
#endif

            for (int x = 0; x < value.Length; x++)
            {
                int offset = startIndex + (structSize * x);
                Struct.FromArray <T>(data, out T result, offset);
                value[x] = result;
            }
        }