From c6b6711eb2ef92d51aa65eb9f5b6ef13c67a5884 Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Sun, 18 Jun 2023 09:52:18 -0400 Subject: [PATCH] refactor[BigEndianBitConverter]: Reduce code reuse in GetBytes methods --- ICD.Common.Utils/BigEndianBitConverter.cs | 36 ++--------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/ICD.Common.Utils/BigEndianBitConverter.cs b/ICD.Common.Utils/BigEndianBitConverter.cs index 67d79c4..668e352 100644 --- a/ICD.Common.Utils/BigEndianBitConverter.cs +++ b/ICD.Common.Utils/BigEndianBitConverter.cs @@ -101,17 +101,7 @@ namespace ICD.Common.Utils } public static byte[] GetBytes(uint value) { - if (!BitConverter.IsLittleEndian) - return BitConverter.GetBytes(value); - - const int total_bytes = sizeof(uint); - - byte[] response = new byte[total_bytes]; - - for (int i = 0; i < total_bytes; i++) - response[i] = (byte)(value >> GetBitShift(i,total_bytes) & FULL_BYTE); - - return response; + return unchecked(GetBytes((int)value)); } public static byte[] GetBytes(short value) @@ -131,17 +121,7 @@ namespace ICD.Common.Utils public static byte[] GetBytes(ushort value) { - if (!BitConverter.IsLittleEndian) - return BitConverter.GetBytes(value); - - const int total_bytes = sizeof(short); - - byte[] response = new byte[total_bytes]; - - for (int i = 0; i < total_bytes; i++) - response[i] = (byte)(value >> GetBitShift(i,total_bytes) & FULL_BYTE); - - return response; + return unchecked(GetBytes((short)value)); } public static byte[] GetBytes(long value) @@ -161,17 +141,7 @@ namespace ICD.Common.Utils public static byte[] GetBytes(ulong value) { - if (!BitConverter.IsLittleEndian) - return BitConverter.GetBytes(value); - - const int total_bytes = sizeof(ulong); - - byte[] response = new byte[total_bytes]; - - for (int i = 0; i < total_bytes; i++) - response[i] = (byte)(value >> GetBitShift(i,total_bytes) & FULL_BYTE); - - return response; + return unchecked(GetBytes((long)value)); } private static int GetBitShift(int byteNumber, int totalBytes) {