Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions source/PlainBytes.System.Extensions/BaseTypes/DoubleExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace PlainBytes.System.Extensions.BaseTypes
Expand Down Expand Up @@ -68,5 +69,48 @@ public static bool IsEqual(this double value, double compared, double tolerance

return Math.Abs(value - compared) < tolerance;
}

/// <summary>
/// Converts the provided doubles into bytes.
/// </summary>
/// <param name="value">Values which should be converted into bytes.</param>
/// <returns>Collection of <see langword="byte"/>s.</returns>
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(this double[] value)
{
ArgumentNullException.ThrowIfNull(value);

var numArray = new byte[value.Length * 8];
Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);
return numArray;
}

/// <summary>
/// Converts the provided bytes into doubles.
/// </summary>
/// <param name="value">Values which should be converted into doubles.</param>
/// <returns>Collection of <see langword="double"/>s.</returns>
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/>.</exception>
/// <exception cref="InvalidDataException">Thrown if the number of bytes does not align with the value type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double[] ToDoubleArray(this byte[] value)
{
ArgumentNullException.ThrowIfNull(value);

if (value.Length % 8 != 0)
{
throw new InvalidDataException("Byte Object length must be a multiple of 8");
}

var result = new double[value.Length / 8];

for (var i = 0; i < value.Length; i += 8)
{
result[i / 8] = BitConverter.ToDouble(value[i..(i + 8)]);
}

return result;
}
}
}
44 changes: 44 additions & 0 deletions source/PlainBytes.System.Extensions/BaseTypes/IntExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace PlainBytes.System.Extensions.BaseTypes
Expand Down Expand Up @@ -37,5 +38,48 @@ public static class IntExtensions
/// <returns><inheritdoc cref="Convert.ToBoolean(uint)"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ToBool(this uint value) => Convert.ToBoolean(value);

/// <summary>
/// Converts the provided ints into bytes.
/// </summary>
/// <param name="value">Values which should be converted into bytes.</param>
/// <returns>Collection of <see langword="byte"/>s.</returns>
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(this int[] value)
{
ArgumentNullException.ThrowIfNull(value);

var numArray = new byte[value.Length * 4];
Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);
return numArray;
}

/// <summary>
/// Converts the provided bytes into ints.
/// </summary>
/// <param name="value">Values which should be converted into ints.</param>
/// <returns>Collection of <see langword="int"/>s.</returns>
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/>.</exception>
/// <exception cref="InvalidDataException">Thrown if the number of bytes does not align with the value type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int[] ToIntArray(this byte[] value)
{
ArgumentNullException.ThrowIfNull(value);

if (value.Length % 4 != 0)
{
throw new InvalidDataException("Byte Object length must be a multiple of 4");
}

var result = new int[value.Length / 4];

for (var i = 0; i < value.Length; i += 4)
{
result[i / 4] = BitConverter.ToInt32(value[i..(i + 4)]);
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions</PackageProjectUrl>
<RepositoryUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions</RepositoryUrl>
<PackageLicenseUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.0.0</Version>
<Version>1.1.0.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<PackageIcon>icon.png</PackageIcon>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using PlainBytes.System.Extensions.BaseTypes;
using Xunit;

namespace PlainBytes.System.Extensions.Tests.BaseTypes
{
public class DoubleExtensionsTests
{
[Fact]
public void Doubles_ToBytes_RoundTrip()
{
// Arrange
var expectedResult = Enumerable.Range(0, 100).Select(_ => Random.Shared.NextDouble()).ToArray();

// Act
var bytes = expectedResult.GetBytes();
var result = bytes.ToDoubleArray();

// Assert
Assert.True(expectedResult.SequenceEqual(result));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using PlainBytes.System.Extensions.BaseTypes;
using Xunit;

namespace PlainBytes.System.Extensions.Tests.BaseTypes
{
public class IntegerExtensionsTests
{
[Fact]
public void Integers_ToBytes_RoundTrip()
{
// Arrange
var expectedResult = Enumerable.Range(0, 100).Select(_ => Random.Shared.Next()).ToArray();

// Act
var bytes = expectedResult.GetBytes();
var result = bytes.ToIntArray();

// Assert
Assert.True(expectedResult.SequenceEqual(result));
}
}
}