As the title explains, this is a series of extension methods that convert certain numeric types to and from byte-arrays, for certain actions which work better on byte-array types than numeric types.
Any and all suggestions are welcome, I am also attaching the Unit Tests.
/// <summary>
/// Provides extension methods to convert certian base types to and from a byte-array.
/// </summary>
public static class NumberByteArrayExtensions
{
/// <summary>
/// Converts a <code>uint</code> value to a <code>byte[]</code>.
/// </summary>
/// <param name="value">The <code>uint</code> value to convert.</param>
/// <returns>A <code>byte[]</code> representing the <code>uint</code> value.</returns>
public static byte[] ToByteArray(this uint value)
{
var size = 4;
var result = new byte[size];
for (var i = 0; i < size; i++)
{
var bitOffset = (size - (i + 1)) * 8;
result[i] = (byte)((value & ((ulong)0xFF << bitOffset)) >> bitOffset);
}
return result;
}
/// <summary>
/// Converts a <code>byte[]</code> to a <code>uint</code> value.
/// </summary>
/// <param name="data">The <code>byte[]</code> to convert.</param>
/// <returns>A <code>uint</code> that represents the converted <code>byte[]</code>.</returns>
public static uint ToUInt32(this byte[] data)
{
var requiredSize = 4;
if (data.Length != requiredSize)
{
throw new ArgumentException($"The byte-array \"{nameof(data)}\" must be exactly {requiredSize} bytes.");
}
var result = 0u;
for (var i = 0; i < requiredSize; i++)
{
result |= ((uint)data[i] << ((requiredSize - (i + 1)) * 8));
}
return result;
}
/// <summary>
/// Converts an <code>int</code> value to a <code>byte[]</code>.
/// </summary>
/// <param name="value">The <code>int</code> to convert.</param>
/// <returns>A <code>byte[]</code> representing the <code>int</code> value.</returns>
public static byte[] ToByteArray(this int value)
{
var t = (uint)value;
return t.ToByteArray();
}
/// <summary>
/// Converts a <code>byte[]</code> to an <code>int</code> value.
/// </summary>
/// <param name="data">The <code>byte[]</code> to convert.</param>
/// <returns>An <code>int</code> value representing the <code>byte[]</code>.</returns>
public static int ToInt32(this byte[] data)
{
var requiredSize = 4;
if (data.Length != requiredSize)
{
throw new ArgumentException($"The byte-array \"{nameof(data)}\" must be exactly {requiredSize} bytes.");
}
return (int)data.ToUInt32();
}
/// <summary>
/// Converts a <code>ulong</code> to a <code>byte[]</code>.
/// </summary>
/// <param name="value">The <code>ulong</code> to convert.</param>
/// <returns>A <code>byte[]</code> representing the <code>ulong</code>.</returns>
public static byte[] ToByteArray(this ulong value)
{
var size = 8;
var result = new byte[size];
for (var i = 0; i < size; i++)
{
var bitOffset = (size - (i + 1)) * 8;
result[i] = (byte)((value & ((ulong)0xFF << bitOffset)) >> bitOffset);
}
return result;
}
/// <summary>
/// Converts a <code>byte[]</code> to a <code>ulong</code>.
/// </summary>
/// <param name="data">The <code>byte[]</code> to convert.</param>
/// <returns>A <code>ulong</code> reprented by the <code>byte[]</code>.</returns>
public static ulong ToUInt64(this byte[] data)
{
var requiredSize = 8;
if (data.Length != requiredSize)
{
throw new ArgumentException($"The byte-array \"{nameof(data)}\" must be exactly {requiredSize} bytes.");
}
var result = 0ul;
for (var i = 0; i < requiredSize; i++)
{
result |= ((ulong)data[i] << ((requiredSize - (i + 1)) * 8));
}
return result;
}
/// <summary>
/// Converts a <code>long</code> value to a <code>byte[]</code>.
/// </summary>
/// <param name="value">The <code>long</code> value to convert.</param>
/// <returns>A <code>byte[]</code> representing the <code>long</code> value.</returns>
public static byte[] ToByteArray(this long value)
{
var t = (ulong)value;
return t.ToByteArray();
}
/// <summary>
/// Converts a <code>byte[]</code> to a <code>long</code> value.
/// </summary>
/// <param name="data">The <code>byte[]</code> to convert.</param>
/// <returns>A <code>long</code> value represented by the <code>byte[]</code>.</returns>
public static long ToInt64(this byte[] data)
{
var requiredSize = 8;
if (data.Length != requiredSize)
{
throw new ArgumentException($"The byte-array \"{nameof(data)}\" must be exactly {requiredSize} bytes.");
}
return (long)data.ToUInt64();
}
}
Unit Tests:
[TestClass]
public class NumberByteArrayExtensionsTests
{
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void UIntToByteArray_0xFF007FBF()
{
var sourceNumber = 0xFF007FBFu;
var resultArray = sourceNumber.ToByteArray();
var expectedResult = new byte[] { 0xFF, 0x00, 0x7F, 0xBF };
CollectionAssert.AreEqual(expectedResult, resultArray);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void ByteArrayToUInt_0xFF_0x00_0x7F_0xBF()
{
var sourceArray = new byte[] { 0xFF, 0x00, 0x7F, 0xBF };
var resultNumber = sourceArray.ToUInt32();
var expectedNumber = 0xFF007FBFu;
Assert.AreEqual(expectedNumber, resultNumber);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void ULongToByteArray_0xFFAF0FCF4F007FBF()
{
var sourceNumber = 0xFFAF0FCF4F007FBFu;
var resultArray = sourceNumber.ToByteArray();
var expectedResult = new byte[] { 0xFF, 0xAF, 0x0F, 0xCF, 0x4F, 0x00, 0x7F, 0xBF };
CollectionAssert.AreEqual(expectedResult, resultArray);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void ByteArrayToULong_0xFF_0xAF_0x0F_0xCF_0x4F_0x00_0x7F_0xBF()
{
var sourceArray = new byte[] { 0xFF, 0xAF, 0x0F, 0xCF, 0x4F, 0x00, 0x7F, 0xBF };
var resultNumber = sourceArray.ToUInt64();
var expectedNumber = 0xFFAF0FCF4F007FBFu;
Assert.AreEqual(expectedNumber, resultNumber);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void IntToByteArray_0x7F00FFBF()
{
var sourceNumber = 0x7F00FFBF;
var resultArray = sourceNumber.ToByteArray();
var expectedResult = new byte[] { 0x7F, 0x00, 0xFF, 0xBF };
CollectionAssert.AreEqual(expectedResult, resultArray);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void ByteArrayToInt_0x7F_0x00_0xFF_0xBF()
{
var sourceArray = new byte[] { 0x7F, 0x00, 0xFF, 0xBF };
var resultNumber = sourceArray.ToInt32();
var expectedNumber = 0x7F00FFBF;
Assert.AreEqual(expectedNumber, resultNumber);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void LongToByteArray_0x7FAF0FCF4F00FFBF()
{
var sourceNumber = 0x7FAF0FCF4F00FFBF;
var resultArray = sourceNumber.ToByteArray();
var expectedResult = new byte[] { 0x7F, 0xAF, 0x0F, 0xCF, 0x4F, 0x00, 0xFF, 0xBF };
CollectionAssert.AreEqual(expectedResult, resultArray);
}
[TestMethod, TestCategory("Number Byte-Array Extensions Tests")]
public void ByteArrayToLong_0x7F_0xAF_0x0F_0xCF_0x4F_0x00_0xFF_0xBF()
{
var sourceArray = new byte[] { 0x7F, 0xAF, 0x0F, 0xCF, 0x4F, 0x00, 0xFF, 0xBF };
var resultNumber = sourceArray.ToInt64();
var expectedNumber = 0x7FAF0FCF4F00FFBF;
Assert.AreEqual(expectedNumber, resultNumber);
}
}
Feel free to review everything (including the tests).