Gapotchenko.FX.Data.Encoding 2022.2.5

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Gapotchenko.FX.Data.Encoding --version 2022.2.5                
NuGet\Install-Package Gapotchenko.FX.Data.Encoding -Version 2022.2.5                
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="2022.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Data.Encoding --version 2022.2.5                
#r "nuget: Gapotchenko.FX.Data.Encoding, 2022.2.5"                
#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=2022.2.5

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

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 semantical 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.

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 decoders and encoders 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 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  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 net46 is compatible.  net461 was computed.  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

The module provides the implementation of data encoding algorithms belonging to Base32 family: Base32, base32-hex, Crockford Base 32, z-base-32.

Gapotchenko.FX.Data.Encoding.Base64

The module provides the implementation of data encoding algorithms belonging to Base64 family: Base64, Base64 URL.

Gapotchenko.FX.Data.Encoding.Base16

The module provides the implementation of data encoding algorithms belonging to Base16 family.

Gapotchenko.FX.Data.Encoding.Base24

The module provides the implementation of data encoding algorithms belonging to Base24 family.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2022.2.7 1,952 5/1/2022
2022.2.5 477 5/1/2022