public static bool EndiannessRequiresReversal(Endiannesses currentEndianness, Endiannesses requiredEndianness) { // We need to reverse the byte arrays if the required endianness of the bytes is different from // whatever order they are currently in // Current | Required | BitConverter.IsLittleEndian | EndiannessRequiresReversal // System | System | N/A | false // System | Little | true | false // System | Little | false | true // System | Big | true | true // System | Big | false | false // Little | System | true | false // Little | System | false | true // Little | Little | N/A | false // Little | Big | N/A | true // Big | System | true | true // Big | System | false | false // Big | Little | N/A | true // Big | Big | N/A | false switch (currentEndianness) { case Endiannesses.Little: return(requiredEndianness == Endiannesses.Big || (requiredEndianness == Endiannesses.System && !BitConverter.IsLittleEndian)); case Endiannesses.Big: return(requiredEndianness == Endiannesses.Little || (requiredEndianness == Endiannesses.System && BitConverter.IsLittleEndian)); default: // System return((requiredEndianness == Endiannesses.Little && !BitConverter.IsLittleEndian) || (requiredEndianness == Endiannesses.Big && BitConverter.IsLittleEndian)); } }
public static byte[] GetBytesFromNumeric <TNumericType>(TNumericType value, Endiannesses requiredEndianness) where TNumericType : struct { Func <object, byte[]> getBytesFunction = GetBytesDictionary[typeof(TNumericType)]; byte[] byteArray = getBytesFunction(value); if (EndiannessRequiresReversal(requiredEndianness)) { Array.Reverse(byteArray); } return(byteArray); }
public static bool EndiannessRequiresReversal(Endiannesses requiredEndianness) { // We need to reverse the byte arrays if the endianness of the bytes is different from the system // Specified | BitConverter.IsLittleEndian | EndiannessRequiresReversal // System | N/A | false // Little | true | false // Little | false | true // Big | true | true // Big | false | false //return (requiredEndianness == Endiannesses.Little && !BitConverter.IsLittleEndian) // || (requiredEndianness == Endiannesses.Big && BitConverter.IsLittleEndian); return(EndiannessRequiresReversal(Endiannesses.System, requiredEndianness)); }
// TODO: Later .Net allows using Enum as a constraint public static byte[] GetBytesFromEnum <TEnumType>(TEnumType value, Endiannesses requiredEndianness) where TEnumType : struct, IConvertible { Type underlyingType = Enum.GetUnderlyingType(typeof(TEnumType)); Func <object, byte[]> getBytesFunction = GetBytesDictionary[underlyingType]; byte[] byteArray = getBytesFunction(value); if (EndiannessRequiresReversal(requiredEndianness)) { Array.Reverse(byteArray); } return(byteArray); }
public static TNumericType GetNumeric <TNumericType>(byte[] array, Endiannesses currentEndianness) where TNumericType : struct { return(GetNumeric <TNumericType>(array, 0, array.Length, currentEndianness)); }
//public static TNumericType GetNumeric<TNumericType>(byte[] fullArray, int startIndex, int length, bool requiresReversal) where TNumericType : struct //{ // Func<byte[], object> parseFunction = GetNumericFromBytesDictionary[typeof(TNumericType)]; // return GetNumeric<TNumericType>(fullArray, startIndex, length, requiresReversal, parseFunction); //} public static TNumericType GetNumeric <TNumericType>(byte[] fullArray, int startIndex, Endiannesses currentEndianness) where TNumericType : struct { int length = System.Runtime.InteropServices.Marshal.SizeOf(typeof(TNumericType)); return(GetNumeric <TNumericType>(fullArray, startIndex, length, currentEndianness)); }
public static TNumericType GetNumeric <TNumericType>(byte[] fullArray, int startIndex, int length, Endiannesses currentEndianness) where TNumericType : struct { Func <byte[], object> parseFunction = GetNumericFromBytesDictionary[typeof(TNumericType)]; return(GetNumeric <TNumericType>(fullArray, startIndex, length, currentEndianness, parseFunction)); }
//public static TNumericType GetNumeric<TNumericType>(byte[] fullArray, int startIndex, int length, bool requiresReversal, Func<byte[], object> parseFunction) where TNumericType : struct //{ // byte[] subArray = GetSubArray(fullArray, startIndex, length, requiresReversal); // TNumericType numericValue = (TNumericType)parseFunction(subArray); // return numericValue; //} public static TEnumType GetEnum <TEnumType>(byte[] fullArray, int startIndex, int length, Endiannesses currentEndianness) where TEnumType : struct, IConvertible { Type underlyingType = Enum.GetUnderlyingType(typeof(TEnumType)); Func <byte[], object> parseFunction = GetNumericFromBytesDictionary[underlyingType]; return(GetNumeric <TEnumType>(fullArray, startIndex, length, currentEndianness, parseFunction)); }
public static TNumericType GetNumeric <TNumericType>(byte[] fullArray, int startIndex, int length, Endiannesses currentEndianness, Func <byte[], object> parseFunction) where TNumericType : struct { //return GetNumeric<TNumericType>(fullArray, startIndex, length, EndiannessRequiresReversal(currentEndianness), parseFunction); byte[] subArray = GetSubArray(fullArray, startIndex, length, EndiannessRequiresReversal(currentEndianness)); TNumericType numericValue = (TNumericType)parseFunction(subArray); return(numericValue); }