HexConvert 1.0.0
See the version list below for details.
dotnet add package HexConvert --version 1.0.0
NuGet\Install-Package HexConvert -Version 1.0.0
<PackageReference Include="HexConvert" Version="1.0.0" />
<PackageVersion Include="HexConvert" Version="1.0.0" />
<PackageReference Include="HexConvert" />
paket add HexConvert --version 1.0.0
#r "nuget: HexConvert, 1.0.0"
#:package HexConvert@1.0.0
#addin nuget:?package=HexConvert&version=1.0.0
#tool nuget:?package=HexConvert&version=1.0.0
HexConvert
Lightweight C# hexadecimal conversion library. This repository contains a small, multi-targeted implementation of helpers to convert between byte arrays and hexadecimal strings with several API flavors: simple string APIs, StringBuilder extension helpers, high-performance Span-based APIs and Trace/Debug helpers. The library is implemented in the System namespace and exposes a static HexConvert helper.
Project layout
src/HexConvert.cs- core string-based APIs:ToHexString,ToBytes,TryParse(lenient parser)src/HexConvert.StringBuilder.cs-StringBuilderextension methods:AppendHex(byte, byte[],ReadOnlySpan<byte>on supported platforms)src/HexConvert.Span.cs- high-performanceSpan-based APIs:GetString,GetChars,GetBytes, pooled helpers and buffer-aware overloadssrc/HexConvert.TraceListener.cs- helpers for writing hex output toTraceListener(WriteHex,WriteHexLine)src/HexConvert.Stream.cs- stream helpers for reading/writing hex text (WriteHex,WriteHexAsync,ReadHex,ReadHexAsync)tests/- unit tests (System.HexConvert.Tests)
Namespace and public types
- Namespace:
System - Static partial class:
HexConvert - Key methods (overview):
string ToHexString(this byte[] bytes, bool uppercase = false, string? separator = null, string? prefixPerByte = null)byte[] ToBytes(this string hex)andbool TryParse(string? hex, out byte[]? bytes)— lenient parsing that ignores whitespace, common separators, and0xprefixes; odd-length input is padded with a leading0StringBuilder.AppendHex(...)overloads for singlebyte,byte[], andReadOnlySpan<byte>(when supported)GetString(ReadOnlySpan<byte>),GetChars(ReadOnlySpan<byte>, Span<char>),GetBytes(ReadOnlySpan<char>, Span<byte>), and pooled variants (returning aRentedArraySegmentWrapper<T>)TraceListener.WriteHex(...)andTraceListener.WriteHexLine(...)overloads forbyte[]andReadOnlySpan<byte>(when supported)Streamhelpers:WriteHex(Stream, byte[]),WriteHexAsync(Stream, byte[]),ReadHex(Stream),ReadHexAsync(Stream)— operate on UTF-8 text and use existing conversion APIsRentedArraySegmentWrapper<T>: a small readonly struct that wraps a rented array segment and returns the array to the pool onDispose()
Behavior and features
- Parsing is lenient and tolerant:
- Ignores whitespace characters and common separators such as
-,:,, - Skips
0x/0Xprefixes (both single leading prefix and per-byte0xsequences) - Accepts odd-length hex input by padding with a leading
0 - Returns
falsefromTryParse(or throwsFormatExceptionfromToBytes) when invalid hex characters are present
- Ignores whitespace characters and common separators such as
- Formatting features:
- Choose uppercase or lowercase hex digits
- Optional separator string between bytes (e.g. space or dash)
- Optional per-byte prefix (e.g.
"0x")
- High-performance span APIs avoid allocations and offer buffer-aware overloads; pooled helpers use
ArrayPool<T>to minimize allocations - Stream helpers read/write UTF-8 text and delegate parsing/formatting to the existing APIs. Async methods are provided where supported. Stream readers/writers use
leaveOpen: trueon platforms that support the parameter to avoid closing the underlying stream.
Examples
Basic string-based conversion
using System;
byte[] data = { 0x01, 0xAF };
string hex = HexConvert.ToHexString(data, uppercase: true, separator: " ", prefixPerByte: "0x");
// hex == "0x01 0xAF"
byte[] parsed = HexConvert.ToBytes("0x01 0xAF");
// or
if (HexConvert.TryParse("0x1 AF", out var bytes)) { /* use bytes */ }
Using StringBuilder extensions
var sb = new System.Text.StringBuilder();
sb.AppendHex(new byte[] { 0x0A, 0xFF }, uppercase: false, separator: ":", prefixPerByte: null);
Console.WriteLine(sb.ToString()); // "0a:ff"
Span-based (high-performance) APIs
ReadOnlySpan<byte> span = new byte[] { 0x01, 0xAB };
string s = HexConvert.GetString(span);
// Get bytes from a hex char span into a pre-allocated buffer
char[] chars = s.ToCharArray();
Span<byte> outBuffer = stackalloc byte[chars.Length / 2 + 1];
int written = HexConvert.GetBytes(chars, outBuffer); // on supported platforms use ReadOnlySpan<char>
Using pooled helpers (remember to dispose)
ReadOnlySpan<char> hexChars = "abcd".AsSpan();
using var rented = HexConvert.GetBytesPooled(hexChars);
byte[] bytes = rented.ArraySegment.Array!; // note: ArraySegment and length available on the wrapper
// When disposed the underlying array is returned to the pool
Trace listener helpers
var listener = new System.Diagnostics.ConsoleTraceListener();
listener.WriteHex(new byte[] { 1, 2, 3 }, uppercase: true, separator: " ", prefixPerByte: "0x");
Stream helpers
// Write to a MemoryStream (synchronous)
using var ms = new MemoryStream();
byte[] data = { 0x01, 0x02 };
ms.WriteHex(data, uppercase: true, separator: " ", prefixPerByte: "0x");
ms.Position = 0;
using var sr = new StreamReader(ms, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: true);
string text = sr.ReadToEnd(); // "0x01 0x02"
// Read from a stream
using var input = new MemoryStream(Encoding.UTF8.GetBytes("0x0A0B"));
byte[] parsed = input.ReadHex(); // { 0x0A, 0x0B }
// Async variants are available on supported platforms: WriteHexAsync / ReadHexAsync
Supported target frameworks
The project in src/System.HexConvert.csproj is multi-targeted and aims to be widely compatible. The repository includes support for many targets (examples):
- .NET Standard 1.0 → 2.1
- .NET Framework 3.5 → 4.8.1
- .NET Core 1.0 → 3.1
- .NET 5 → 10
Pick the appropriate target when referencing the library.
Build and test
Requires the .NET SDK. Recommended: .NET 7 or later.
From the repository root:
dotnet restore
dotnet build
dotnet test
License
References
- Original inspiration: https://github.com/faustodavid/HexConverter
- This repository: https://github.com/emako/HexConvert
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp1.0 is compatible. netcoreapp1.1 is compatible. netcoreapp2.0 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 is compatible. netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard1.0 is compatible. netstandard1.1 is compatible. netstandard1.2 is compatible. netstandard1.3 is compatible. netstandard1.4 is compatible. netstandard1.5 is compatible. netstandard1.6 is compatible. netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 is compatible. net452 is compatible. net46 is compatible. net461 is compatible. net462 is compatible. net463 was computed. net47 is compatible. net471 is compatible. net472 is compatible. net48 is compatible. net481 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
| Universal Windows Platform | uap was computed. uap10.0 was computed. |
| Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
| Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 1.0
- Microsoft.NETCore.App (>= 1.0.5)
- System.Memory (>= 4.5.4)
-
.NETCoreApp 1.1
- Microsoft.NETCore.App (>= 1.1.2)
- System.Memory (>= 4.5.4)
-
.NETCoreApp 2.0
- System.Memory (>= 4.5.4)
-
.NETCoreApp 2.1
- No dependencies.
-
.NETCoreApp 2.2
- No dependencies.
-
.NETCoreApp 3.0
- No dependencies.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- System.Memory (>= 4.5.4)
-
.NETFramework 4.5.1
- System.Memory (>= 4.5.4)
-
.NETFramework 4.5.2
- System.Memory (>= 4.5.4)
-
.NETFramework 4.6
- System.Memory (>= 4.5.4)
-
.NETFramework 4.6.1
- System.Memory (>= 4.5.4)
-
.NETFramework 4.6.2
- System.Memory (>= 4.5.4)
-
.NETFramework 4.7
- System.Memory (>= 4.5.4)
-
.NETFramework 4.7.1
- System.Memory (>= 4.5.4)
-
.NETFramework 4.7.2
- System.Memory (>= 4.5.4)
-
.NETFramework 4.8
- System.Memory (>= 4.5.4)
-
.NETFramework 4.8.1
- System.Memory (>= 4.5.4)
-
.NETStandard 1.0
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.1
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 1.2
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 1.4
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 1.5
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 1.6
- NETStandard.Library (>= 1.6.1)
- System.Memory (>= 4.5.4)
-
.NETStandard 2.0
- System.Memory (>= 4.5.4)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
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.