Skip to content

Commit 5056eb7

Browse files
authored
Replace BitConverter with BinaryPrimitives (#124)
1 parent 8eb3adf commit 5056eb7

1 file changed

Lines changed: 8 additions & 126 deletions

File tree

src/ZWave/BinaryExtensions.cs

Lines changed: 8 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,18 @@
1-
namespace ZWave;
1+
using System.Buffers.Binary;
2+
3+
namespace ZWave;
24

35
internal static class BinaryExtensions
46
{
57
public static sbyte ToInt8(this byte b) => unchecked((sbyte)b);
68

7-
public static ushort ToUInt16BE(this ReadOnlySpan<byte> bytes)
8-
{
9-
if (bytes.Length > sizeof(ushort))
10-
{
11-
throw new ArgumentException($"The number of bytes ({bytes.Length}) is more than can fit in an ushort ({sizeof(ushort)}).", nameof(bytes));
12-
}
13-
14-
// BitConverter uses the endianness of the machine, so figure out if we have to reverse the bytes.
15-
if (BitConverter.IsLittleEndian)
16-
{
17-
// Note: There is no need to pad since LE would be padded on the right.
18-
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
19-
bytes.CopyTo(buffer);
20-
buffer.Reverse();
21-
return BitConverter.ToUInt16(buffer);
22-
}
23-
else if (bytes.Length < sizeof(ushort))
24-
{
25-
// Add padding
26-
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
27-
bytes.CopyTo(buffer.Slice(sizeof(ushort) - bytes.Length));
28-
return BitConverter.ToUInt16(buffer);
29-
}
30-
else
31-
{
32-
// Perfect size and endianness
33-
return BitConverter.ToUInt16(bytes);
34-
}
35-
}
36-
37-
public static void WriteBytesBE(this ushort value, Span<byte> destination)
38-
{
39-
if (destination.Length != sizeof(ushort))
40-
{
41-
throw new ArgumentException($"Destination must be of length {sizeof(ushort)}");
42-
}
43-
44-
if (!BitConverter.TryWriteBytes(destination, value))
45-
{
46-
// This really should never happen.
47-
throw new InvalidOperationException($"Value {value} could not be converted to bytes.");
48-
}
49-
50-
// BitConverter uses the endianness of the machine, so figure out if we have to reverse the bytes.
51-
if (BitConverter.IsLittleEndian)
52-
{
53-
destination.Reverse();
54-
}
55-
}
56-
57-
public static uint ToUInt32BE(this ReadOnlySpan<byte> bytes)
58-
{
59-
if (bytes.Length > sizeof(uint))
60-
{
61-
throw new ArgumentException($"The number of bytes ({bytes.Length}) is more than can fit in an uint ({sizeof(uint)}).", nameof(bytes));
62-
}
63-
64-
// BitConverter uses the endianness of the machine, so figure out if we have to reverse the bytes.
65-
if (BitConverter.IsLittleEndian)
66-
{
67-
// Note: There is no need to pad since LE would be padded on the right.
68-
Span<byte> buffer = stackalloc byte[sizeof(uint)];
69-
bytes.CopyTo(buffer);
70-
buffer.Reverse();
71-
return BitConverter.ToUInt32(buffer);
72-
}
73-
else if (bytes.Length < sizeof(uint))
74-
{
75-
// Add padding
76-
Span<byte> buffer = stackalloc byte[sizeof(uint)];
77-
bytes.CopyTo(buffer.Slice(sizeof(uint) - bytes.Length));
78-
return BitConverter.ToUInt32(buffer);
79-
}
80-
else
81-
{
82-
// Perfect size and endianness
83-
return BitConverter.ToUInt32(bytes);
84-
}
85-
}
86-
87-
public static void WriteBytesBE(this uint value, Span<byte> destination)
88-
{
89-
if (destination.Length != sizeof(uint))
90-
{
91-
throw new ArgumentException($"Destination must be of length {sizeof(uint)}");
92-
}
9+
public static ushort ToUInt16BE(this ReadOnlySpan<byte> bytes) => BinaryPrimitives.ReadUInt16BigEndian(bytes);
9310

94-
if (!BitConverter.TryWriteBytes(destination, value))
95-
{
96-
// This really should never happen.
97-
throw new InvalidOperationException($"Value {value} could not be converted to bytes.");
98-
}
11+
public static void WriteBytesBE(this ushort value, Span<byte> destination) => BinaryPrimitives.WriteUInt16BigEndian(destination, value);
9912

100-
// BitConverter uses the endianness of the machine, so figure out if we have to reverse the bytes.
101-
if (BitConverter.IsLittleEndian)
102-
{
103-
destination.Reverse();
104-
}
105-
}
13+
public static uint ToUInt32BE(this ReadOnlySpan<byte> bytes) => BinaryPrimitives.ReadUInt32BigEndian(bytes);
10614

107-
public static int ToInt32BE(this ReadOnlySpan<byte> bytes)
108-
{
109-
if (bytes.Length > sizeof(int))
110-
{
111-
throw new ArgumentException($"The number of bytes ({bytes.Length}) is more than can fit in an int ({sizeof(int)}).", nameof(bytes));
112-
}
15+
public static void WriteBytesBE(this uint value, Span<byte> destination) => BinaryPrimitives.WriteUInt32BigEndian(destination, value);
11316

114-
// BitConverter uses the endianness of the machine, so figure out if we have to reverse the bytes.
115-
if (BitConverter.IsLittleEndian)
116-
{
117-
// Note: There is no need to pad since LE would be padded on the right.
118-
Span<byte> buffer = stackalloc byte[sizeof(int)];
119-
bytes.CopyTo(buffer);
120-
buffer.Reverse();
121-
return BitConverter.ToInt32(buffer);
122-
}
123-
else if (bytes.Length < sizeof(int))
124-
{
125-
// Add padding
126-
Span<byte> buffer = stackalloc byte[sizeof(int)];
127-
bytes.CopyTo(buffer.Slice(sizeof(int) - bytes.Length));
128-
return BitConverter.ToInt32(buffer);
129-
}
130-
else
131-
{
132-
// Perfect size and endianness
133-
return BitConverter.ToInt32(bytes);
134-
}
135-
}
17+
public static int ToInt32BE(this ReadOnlySpan<byte> bytes) => BinaryPrimitives.ReadInt32BigEndian(bytes);
13618
}

0 commit comments

Comments
 (0)