Gapotchenko.FX.Data.Encoding 2024.1.3

Prefix Reserved
dotnet add package Gapotchenko.FX.Data.Encoding --version 2024.1.3                
NuGet\Install-Package Gapotchenko.FX.Data.Encoding -Version 2024.1.3                
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="Gapotchenko.FX.Data.Encoding" Version="2024.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Data.Encoding --version 2024.1.3                
#r "nuget: Gapotchenko.FX.Data.Encoding, 2024.1.3"                
#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.
// Install Gapotchenko.FX.Data.Encoding as a Cake Addin
#addin nuget:?package=Gapotchenko.FX.Data.Encoding&version=2024.1.3

// Install Gapotchenko.FX.Data.Encoding as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Data.Encoding&version=2024.1.3                

Overview

Gapotchenko.FX.Data.Encoding module provides a quasi-universal framework for data encodings. It supports both synchronous and asynchronous data processing models, including the iterative transcoding.

The module serves as the implementation basis for a variety of popular encodings: Base16, Base32, Base64, and others.

ITextDataEncoding

This is the root interface provided by a binary-to-text data encoding algorithm. The interface has several notable methods.

GetString(ReadOnlySpan<byte> data)

This method encodes all the bytes in the specified span into a string.

For example, GetString method of a Base16 encoding would be used like this:

using Gapotchenko.FX.Data.Encoding;

var data = new byte[] { 1, 2, 3, 10 };
var s = Base16.GetString(data);
Console.WriteLine(s);

producing the following output:

0102030A

The GetString method can also take options. Here is an example that would produce the indented output:

var data = new byte[] { 1, 2, 3, 10 };
var s = Base16.GetString(data, DataEncodingOptions.Indent);
Console.WriteLine(s);

The output:

01 02 03 0A

Note that the output now contains space separators (indentations) between the encoded data values. Not all encodings support indentation so Indent option flag may be ignored by some of them.

GetBytes(ReadOnlySpan<char> s)

The method decodes all the characters in the specified read-only span into a byte array. In this way, GetBytes method performs a reverse operation to GetString.

For example, GetBytes method of a Base16 encoding can be used like this:

using Gapotchenko.FX.Data.Encoding;

byte[] data = Base16.GetBytes("0102030A");
foreach (var i in data)
    Console.WriteLine(i);

producing the following output:

1
2
3
10

CreateEncoder(TextWriter textWriter)

The method creates a streaming encoder for the specified binary-to-text data encoding.

Example:

using Gapotchenko.FX.Data.Encoding;
using System.IO;

var encoding = Base16.Instance;

// Create a destination text writer.
var sw = new StringWriter();

// Create a streaming encoder.
var stream = encoding.CreateEncoder(sw);

// Stream the data.
stream.Write(1);
stream.Write(2);
stream.Write(3);
stream.Write(10);

// Flush the data.
stream.Flush();

Console.WriteLine(sw.ToString());

The output:

0102030A

It is worth mentioning that the streaming encoder also supports the asynchronous operations.

CreateDecoder(TextReader textReader)

The method creates a streaming decoder for the specified binary-to-text data encoding. It can be viewed as a reverse operation to CreateEncoder and it fully supports the asynchronous operations as well.

Transcoding Between Various Binary-To-Text Encodings

Imagine a Base64-encoded file that needs to be converted to Base32. The file is pretty large, around 2 gigabytes of data.

A naive approach would be to read all the data from the file beforehand in order to re-encode it later:

using Gapotchenko.FX.Data.Encoding;
using System.IO;

// Read the file.
var text = File.ReadAllText("2GB-base64.txt");

// Decode the Base64 data.
var data = Base64.GetBytes(text);

// Re-encode the data with Base32 encoding.
text = Base32.GetString(data);

// Save the new file.
File.WriteAllText("2GB-base32.txt", text);

It will work but obviously will consume at least 2 GB of RAM.

This is when the concept of streaming becomes handy. A better transcoding algorithm can use just a fraction of RAM to perform the very same operation of re-encoding a Base64-encoded file to Base32:

using Gapotchenko.FX.Data.Encoding;
using System.IO;

var sourceEncoding = Base64.Instance;
var destinationEncoding = Base32.Instance;

// Open the source file.
using var sourceTextReader = File.OpenText("2GB-base64.txt");
// Create the destination file.
using var destinationTextWriter = File.CreateText("2GB-base32.txt");

// Create a streaming decoder for the Base64-encoded source file.
var sourceStream = sourceEncoding.CreateDecoder(sourceTextReader);
// Create a streaming encoder for the Base32-encoded destination file.
var destinationStream = destinationEncoding.CreateEncoder(destinationTextWriter);

// Transcode the file by copying the source stream to destination.
sourceStream.CopyTo(destinationStream);
destinationStream.Flush();

The transcoding algorithm based on streaming codecs presented above is highly efficient in terms of memory usage and consumes just a few kilobytes of RAM to transcode a file of any size.

Data Encoding Algorithms

Gapotchenko.FX.Data.Encoding module provides only the framework for data encoding algorithms. If you want to use a ready-to-use algorithm, Gapotchenko.FX provides quite a few out of the box:

Algorithm Family Module Algorithms
Base16 Gapotchenko.FX.Data.Encoding.Base16 Base16
Base24 Gapotchenko.FX.Data.Encoding.Base24 Kuon Base24
Base32 Gapotchenko.FX.Data.Encoding.Base32 Base32, base32-hex, Crockford Base 32, z-base-32
Base64 Gapotchenko.FX.Data.Encoding.Base64 Base64, Base64 URL

Moreover, you can create your own data encoding algorithm. Gapotchenko.FX project welcomes contributions, or it can be a standalone NuGet package that uses Gapotchenko.FX.Data.Encoding module as a wireframe.

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.

Product 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Gapotchenko.FX.Data.Encoding:

Package Downloads
Gapotchenko.FX.Data.Encoding.Base32

Provides Base32 data encoding algorithms: Base32, base32-hex, Crockford Base 32, z-base-32.

Gapotchenko.FX.Data.Encoding.Base64

Provides Base64 data encoding algorithms: Base64, Base64 URL.

Gapotchenko.FX.Data.Encoding.Base16

Provides Base16 data encoding algorithms.

Gapotchenko.FX.Data.Encoding.Base24

Provides Base24 data encoding algorithms.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.1.3 201 11/10/2024
2022.2.7 1,984 5/1/2022
2022.2.5 490 5/1/2022