![]() |
VOOZH | about |
dotnet add package MrKWatkins.BinaryPrimitives --version 1.6.0
NuGet\Install-Package MrKWatkins.BinaryPrimitives -Version 1.6.0
<PackageReference Include="MrKWatkins.BinaryPrimitives" Version="1.6.0" />
<PackageVersion Include="MrKWatkins.BinaryPrimitives" Version="1.6.0" />Directory.Packages.props
<PackageReference Include="MrKWatkins.BinaryPrimitives" />Project file
paket add MrKWatkins.BinaryPrimitives --version 1.6.0
#r "nuget: MrKWatkins.BinaryPrimitives, 1.6.0"
#:package MrKWatkins.BinaryPrimitives@1.6.0
#addin nuget:?package=MrKWatkins.BinaryPrimitives&version=1.6.0Install as a Cake Addin
#tool nuget:?package=MrKWatkins.BinaryPrimitives&version=1.6.0Install as a Cake Tool
👁 Build Status
👁 NuGet Version
👁 NuGet Downloads
Extension methods on primitives types for various binary operations, such as reading and writing values from bytes and calculating parity.
The name is based on the BinaryPrimitives class in the .NET Framework which provides similar methods.
Full documentation is available at mrkwatkins.github.io/BinaryPrimitives. Source code and issue tracking are on GitHub.
dotnet add package MrKWatkins.BinaryPrimitives
Read and write multi-byte integers from byte[], Span<byte>, Stream, and other byte
container types. All methods support an optional Endian parameter (defaulting to
Endian.Little):
byte[] data = new byte[8];
data.SetInt32(0, 42);
data.SetUInt16(4, 1000, Endian.Big);
int a = data.GetInt32(0); // 42
ushort b = data.GetUInt16(4, Endian.Big); // 1000
Collections can have values appended directly:
var buffer = new List<byte>();
buffer.AddUInt32(0xDEADBEEF);
buffer.AddInt16(-1, Endian.Big);
Streams support typed reading and writing, with async overloads for all methods:
stream.WriteInt32(42);
int value = stream.ReadInt32OrThrow();
Get, set, and clear individual bits or ranges of bits on byte, ushort, int, uint,
long, and ulong:
byte b = 0b00001010;
bool set = b.GetBit(1); // true
byte with0 = b.SetBit(0); // 0b00001011
byte clear = b.ResetBit(1); // 0b00001000
byte range = b.GetBits(1, 3); // bits 1..3, shifted to position 0
byte also provides nibble access, parity, and CPU flag detection:
byte high = b.HighNibble(); // bits 4-7
bool even = b.Parity(); // true if an even number of bits are set
All supported integer types can be formatted as a binary string:
string s = ((byte)0x5A).ToBinaryString(); // "0b01011010"
ReadOnlyListStream wraps an IReadOnlyList<byte> as a seekable, read-only Stream:
IReadOnlyList<byte> bytes = LoadBytes();
using var stream = new ReadOnlyListStream(bytes);
PeekableStream wraps any readable Stream to add one-byte lookahead without consuming the
byte:
using var stream = new PeekableStream(inner);
if (stream.Peek() == 0xFF)
{
stream.ReadByteOrThrow(); // consume it
}
Licensed under GPL v3.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 5 NuGet packages that depend on MrKWatkins.BinaryPrimitives:
| Package | Downloads |
|---|---|
|
MrKWatkins.OakAsm
Shared code for the OakAsm project. |
|
|
MrKWatkins.OakAsm.Z80
Library containing assembly definitions for the Z80 CPU, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.Formatting
Library for formatting assembly code, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.Parsing
Library for parsing assembly code, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.IO
Package Description |
This package is not used by any popular GitHub repositories.