-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolExtensions.cs
More file actions
30 lines (26 loc) · 1005 Bytes
/
Copy pathBoolExtensions.cs
File metadata and controls
30 lines (26 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace Texnomic.Types.Extensions;
/// <summary>
/// Extension methods for <see cref="bool"/> values.
/// </summary>
public static class BoolExtensions
{
extension(bool Value)
{
/// <summary>Encodes the boolean as a single-byte array (<c>0x01</c> / <c>0x00</c>).</summary>
public byte[] ToBytes() => new[] { Convert.ToByte(Value) };
/// <summary>Attempts to write the boolean as a single byte (<c>0x01</c> / <c>0x00</c>).</summary>
public bool TryWriteBytes(Span<byte> Destination, out int BytesWritten)
{
if (Destination.Length < 1)
{
BytesWritten = 0;
return false;
}
Destination[0] = Convert.ToByte(Value);
BytesWritten = 1;
return true;
}
/// <summary>Returns the canonical hexadecimal string <c>0x1</c> for <c>true</c> and <c>0x0</c> for <c>false</c>.</summary>
public string AsHex() => Value ? "0x1" : "0x0";
}
}