Skip to content

Commit bc8090b

Browse files
authored
Merge pull request #17 from PlainBytes/feature/double-and-int-to-bytes
Adds conversion to bytes for double and int types
2 parents 6589116 + 3ddb84b commit bc8090b

5 files changed

Lines changed: 137 additions & 1 deletion

File tree

source/PlainBytes.System.Extensions/BaseTypes/DoubleExtensions.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Runtime.CompilerServices;
34

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

6970
return Math.Abs(value - compared) < tolerance;
7071
}
72+
73+
/// <summary>
74+
/// Converts the provided doubles into bytes.
75+
/// </summary>
76+
/// <param name="value">Values which should be converted into bytes.</param>
77+
/// <returns>Collection of <see langword="byte"/>s.</returns>
78+
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/></exception>
79+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80+
public static byte[] GetBytes(this double[] value)
81+
{
82+
ArgumentNullException.ThrowIfNull(value);
83+
84+
var numArray = new byte[value.Length * 8];
85+
Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);
86+
return numArray;
87+
}
88+
89+
/// <summary>
90+
/// Converts the provided bytes into doubles.
91+
/// </summary>
92+
/// <param name="value">Values which should be converted into doubles.</param>
93+
/// <returns>Collection of <see langword="double"/>s.</returns>
94+
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/>.</exception>
95+
/// <exception cref="InvalidDataException">Thrown if the number of bytes does not align with the value type.</exception>
96+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97+
public static double[] ToDoubleArray(this byte[] value)
98+
{
99+
ArgumentNullException.ThrowIfNull(value);
100+
101+
if (value.Length % 8 != 0)
102+
{
103+
throw new InvalidDataException("Byte Object length must be a multiple of 8");
104+
}
105+
106+
var result = new double[value.Length / 8];
107+
108+
for (var i = 0; i < value.Length; i += 8)
109+
{
110+
result[i / 8] = BitConverter.ToDouble(value[i..(i + 8)]);
111+
}
112+
113+
return result;
114+
}
71115
}
72116
}

source/PlainBytes.System.Extensions/BaseTypes/IntExtensions.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Runtime.CompilerServices;
34

45
namespace PlainBytes.System.Extensions.BaseTypes
@@ -37,5 +38,48 @@ public static class IntExtensions
3738
/// <returns><inheritdoc cref="Convert.ToBoolean(uint)"/></returns>
3839
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3940
public static bool ToBool(this uint value) => Convert.ToBoolean(value);
41+
42+
/// <summary>
43+
/// Converts the provided ints into bytes.
44+
/// </summary>
45+
/// <param name="value">Values which should be converted into bytes.</param>
46+
/// <returns>Collection of <see langword="byte"/>s.</returns>
47+
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/></exception>
48+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49+
public static byte[] GetBytes(this int[] value)
50+
{
51+
ArgumentNullException.ThrowIfNull(value);
52+
53+
var numArray = new byte[value.Length * 4];
54+
Buffer.BlockCopy(value, 0, numArray, 0, numArray.Length);
55+
return numArray;
56+
}
57+
58+
/// <summary>
59+
/// Converts the provided bytes into ints.
60+
/// </summary>
61+
/// <param name="value">Values which should be converted into ints.</param>
62+
/// <returns>Collection of <see langword="int"/>s.</returns>
63+
/// <exception cref="ArgumentNullException">Thrown if the provided value is <see langword="null"/>.</exception>
64+
/// <exception cref="InvalidDataException">Thrown if the number of bytes does not align with the value type.</exception>
65+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
66+
public static int[] ToIntArray(this byte[] value)
67+
{
68+
ArgumentNullException.ThrowIfNull(value);
69+
70+
if (value.Length % 4 != 0)
71+
{
72+
throw new InvalidDataException("Byte Object length must be a multiple of 4");
73+
}
74+
75+
var result = new int[value.Length / 4];
76+
77+
for (var i = 0; i < value.Length; i += 4)
78+
{
79+
result[i / 4] = BitConverter.ToInt32(value[i..(i + 4)]);
80+
}
81+
82+
return result;
83+
}
4084
}
4185
}

source/PlainBytes.System.Extensions/PlainBytes.System.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<PackageProjectUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions</RepositoryUrl>
1414
<PackageLicenseUrl>https://github.com/PlainBytes/PlainBytes.System.Extensions/blob/master/LICENSE</PackageLicenseUrl>
15-
<Version>1.0.0.0</Version>
15+
<Version>1.1.0.0</Version>
1616
<AssemblyVersion>$(Version)</AssemblyVersion>
1717
<FileVersion>$(Version)</FileVersion>
1818
<PackageIcon>icon.png</PackageIcon>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Linq;
3+
using PlainBytes.System.Extensions.BaseTypes;
4+
using Xunit;
5+
6+
namespace PlainBytes.System.Extensions.Tests.BaseTypes
7+
{
8+
public class DoubleExtensionsTests
9+
{
10+
[Fact]
11+
public void Doubles_ToBytes_RoundTrip()
12+
{
13+
// Arrange
14+
var expectedResult = Enumerable.Range(0, 100).Select(_ => Random.Shared.NextDouble()).ToArray();
15+
16+
// Act
17+
var bytes = expectedResult.GetBytes();
18+
var result = bytes.ToDoubleArray();
19+
20+
// Assert
21+
Assert.True(expectedResult.SequenceEqual(result));
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Linq;
3+
using PlainBytes.System.Extensions.BaseTypes;
4+
using Xunit;
5+
6+
namespace PlainBytes.System.Extensions.Tests.BaseTypes
7+
{
8+
public class IntegerExtensionsTests
9+
{
10+
[Fact]
11+
public void Integers_ToBytes_RoundTrip()
12+
{
13+
// Arrange
14+
var expectedResult = Enumerable.Range(0, 100).Select(_ => Random.Shared.Next()).ToArray();
15+
16+
// Act
17+
var bytes = expectedResult.GetBytes();
18+
var result = bytes.ToIntArray();
19+
20+
// Assert
21+
Assert.True(expectedResult.SequenceEqual(result));
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)