Plugin.ByteArrays 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Plugin.ByteArrays --version 1.0.6
                    
NuGet\Install-Package Plugin.ByteArrays -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.ByteArrays" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Plugin.ByteArrays" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Plugin.ByteArrays" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Plugin.ByteArrays --version 1.0.6
                    
#r "nuget: Plugin.ByteArrays, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Plugin.ByteArrays@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Plugin.ByteArrays&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Plugin.ByteArrays&version=1.0.6
                    
Install as a Cake Tool

๐Ÿ—‚๏ธ Plugin.ByteArrays

CI .NET NuGet NuGet Downloads GitHub Release License GitHub Pages

A comprehensive set of utilities for working with byte arrays in .NET applications. These helpers are designed for performance, safety, and ease of use across all supported platforms.


๐Ÿ“ฆ Features

  • ByteArrayBuilder: Fluently build byte arrays from various types and encodings
  • ByteArrayExtensions: Extension methods for reading primitives, strings, and complex types from byte arrays
  • Array Manipulation: Safe operations like slicing, concatenation, trimming, reversing, and XOR
  • Pattern Matching: Search and compare byte arrays with StartsWith, EndsWith, IndexOf, and equality checks
  • Format Conversion: Convert to/from hex strings, Base64, ASCII, and UTF-8 encodings
  • Object Serialization: Convert objects and primitives to byte arrays with type safety
  • Safe Operations: OrDefault methods that never throw exceptions and bounds-checked operations

๐Ÿ› ๏ธ Usage Examples

ByteArrayBuilder

using Plugin.ByteArrays;

using var builder = new ByteArrayBuilder();
builder.Append(0x01)
       .AppendUtf8String("Hello")
       .Append(new byte[] { 0x02, 0x03 })
       .Append(42)
       .AppendHexString("DEADBEEF");

byte[] result = builder.ToByteArray();

Reading Primitives from Byte Arrays

using Plugin.ByteArrays;

byte[] data = { 0x01, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F };
var position = 0;

bool flag = data.ToBoolean(ref position);        // reads 1 byte
int value = data.ToInt32(ref position);          // reads 4 bytes, advances position
string text = data.ToUtf8String(ref position, 5); // reads 5 bytes

// Safe variants that don't throw
var safeValue = data.ToInt16OrDefault(ref position, defaultValue: -1);

Array Manipulation

using Plugin.ByteArrays;

byte[] data = { 0x10, 0x20, 0x30, 0x40, 0x00, 0x00 };

// Safe slicing
byte[] slice = data.SafeSlice(1, 3);  // [0x20, 0x30, 0x40]

// Concatenation
byte[] a = { 0x01, 0x02 };
byte[] b = { 0x03, 0x04 };
byte[] combined = ByteArrayExtensions.Concatenate(a, b);  // [0x01, 0x02, 0x03, 0x04]

// Trimming trailing zeros
byte[] trimmed = data.TrimEndNonDestructive();  // [0x10, 0x20, 0x30, 0x40]

// Reverse and XOR operations
byte[] reversed = data.Reverse();
byte[] xorResult = a.Xor(new byte[] { 0xFF, 0xFF });
using Plugin.ByteArrays;

byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
byte[] pattern = { 0x65, 0x6C };

bool starts = data.StartsWith(new byte[] { 0x48, 0x65 });  // true
bool ends = data.EndsWith(new byte[] { 0x6C, 0x6F });      // true
int index = data.IndexOf(pattern);                         // 1

// Array comparison
byte[] other = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
bool identical = data.IsIdenticalTo(other);                // true

String and Format Conversions

using Plugin.ByteArrays;

// String to byte array
byte[] utf8Bytes = "Hello World".Utf8StringToByteArray();
byte[] asciiBytes = "Hello".AsciiStringToByteArray();
byte[] hexBytes = "48656C6C6F".HexStringToByteArray();     // "Hello" in hex
byte[] base64Bytes = "SGVsbG8=".Base64StringToByteArray(); // "Hello" in base64

// Byte array to string
string hex = data.ToHexString("-", "0x");  // "0x48-0x65-0x6C-0x6C-0x6F"
string base64 = data.ToBase64String();
string utf8 = data.ToUtf8String(ref position, length: 5);

Object Serialization

using Plugin.ByteArrays;

// Convert any supported type to byte array
int number = 42;
byte[] numberBytes = number.ToByteArray();

DateTime now = DateTime.Now;
byte[] dateBytes = now.ToByteArray();

// Enums are supported
MyEnum enumValue = MyEnum.SomeValue;
byte[] enumBytes = enumValue.ToByteArray();

๐Ÿ“‹ API Overview

Core Classes

  • ByteArrayBuilder - Fluent builder for constructing byte arrays
  • ByteArrayExtensions - Extension methods for reading and manipulating byte arrays
  • ObjectToByteArrayExtensions - Object-to-byte-array conversion helpers

Reading Operations

  • Primitives: ToBoolean, ToByte, ToSByte, ToChar, ToInt16/32/64, ToUInt16/32/64
  • Floating Point: ToSingle, ToDouble, ToHalf
  • Strings: ToUtf8String, ToAsciiString, ToHexString, ToBase64String
  • Complex Types: ToEnum<T>, ToVersion
  • Safe Variants: All methods have OrDefault versions that return defaults instead of throwing

Writing Operations

  • Fluent Building: Append<T>, AppendUtf8String, AppendAsciiString, AppendHexString, AppendBase64String
  • Direct Conversion: Utf8StringToByteArray, HexStringToByteArray, ToByteArray<T>

Array Operations

  • Manipulation: SafeSlice, Concatenate, TrimEnd, TrimEndNonDestructive, Reverse, Xor
  • Pattern Matching: StartsWith, EndsWith, IndexOf, IsIdenticalTo
  • Debugging: ToDebugString, ToHexDebugString

๐Ÿ”ง Design Principles

  • Safety First: Explicit bounds checking with clear exception messages
  • Zero-Allocation Friendly: Efficient operations using ReadOnlySpan<byte> and SequenceEqual
  • Fail-Safe Defaults: OrDefault methods never advance read cursors on failure
  • Type Safety: Strict enum conversion with validation for undefined values
  • Cross-Platform: Optimized for .NET 9 and modern C# features

๐Ÿงช Development

  • Testing: xUnit + FluentAssertions with comprehensive coverage
  • Build: dotnet build
  • Test: dotnet test
  • Framework: .NET 9.0

๐Ÿ“„ License

MIT โ€” see LICENSE.md for details.


Designed for modern .NET applications requiring efficient, safe byte array operations.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.15 277 9/18/2025
1.0.14 280 9/18/2025
1.0.13 273 9/17/2025
1.0.12 287 9/16/2025
1.0.11 282 9/16/2025
1.0.10 281 9/16/2025
1.0.9 272 9/16/2025
1.0.8 272 9/16/2025
1.0.7 274 9/16/2025
1.0.6 271 9/16/2025
1.0.5 272 9/15/2025