Beispiel #1
0
 void GetBytesInternal(T[] items, int sizeOfItem, bool marshalElements, Span <byte> currentItem, Span <byte> resultSpan)
 {
     for (int x = 0; x < items.Length; x++)
     {
         Struct.GetBytes(ref items[x], marshalElements, currentItem);
         currentItem.CopyTo(resultSpan);
         resultSpan = resultSpan.Slice(sizeOfItem);
     }
 }
Beispiel #2
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);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a byte array from specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="items">The item to convert into a byte array.</param>
        /// <param name="marshalElements">Set to true to marshal the item(s).</param>
        public static byte[] GetBytes <T>(T[] items, bool marshalElements = false)
        {
            int         totalSize = GetSize <T>(items.Length);
            List <byte> array     = new List <byte>(totalSize);

            for (int x = 0; x < items.Length; x++)
            {
                array.AddRange(Struct.GetBytes(ref items[x]));
            }

            return(array.ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Creates a byte array from specified structure or class type with explicit StructLayout attribute.
        /// </summary>
        /// <param name="items">The item to convert into a byte array.</param>
        public static byte[] GetBytes <T>(T[] items) where T : unmanaged
        {
            int         totalSize = GetSize <T>(items.Length);
            List <byte> array     = new List <byte>(totalSize);

            for (int x = 0; x < items.Length; x++)
            {
                array.AddRange(Struct.GetBytes(ref items[x]));
            }

            return(array.ToArray());
        }