BaseN encoding
BaseN encodings are used to encode binary data into text. Where N is the size of the text alphabet used for encoding. BaseN encodings are used to "prepare data" for transmission via text protocols (HTTP, SMTP) or non-digital channels (eg printing on paper).
The most popular variant of this encoding is Base64 . Only Base16 known as hexadecimal or simply hex can be compared in frequency of use . Tools for working with them have been in .NET since the first versions, but they are not very convenient.
What's in the .NET Framework (Core)
System.Security.Cryptography
ToBase64Transform / FromBase64Transform
A couple of classes to convert to / from Base64. Using the CryptoStream helper class, it can be used to stream process Stream.
using System.Security.Cryptography;
var base64Transform = new ToBase64Transform();
var output = new MemoryStream();
var base64Encoder = new CryptoStream(output, base64Transform, CryptoStreamMode.Write);
base64Encoder.Write(new byte[] { 122 }, 0, 1);
base64Encoder.Close();
// output.ToArray() -> byte[4] { 101, 103, 61, 61 } aka "eg=="
System.Convert
ToBase64String / FromBase64String
A couple of functions to convert an array of bytes to a Base64 string and back.
using System;
var bytes = System.Convert.FromBase64String("eg==")
// bytes -> 122
System.BitConverter
A function to convert an array of bytes to a Base16 (hexadecimal) string, where each byte is separated by a hyphen. Example: 0A-C0-D3
using System;
var hexString = BitConverter.ToString(new byte[] { 122, 122 });
// hexString -> "7A-7A"
System.Runtime.Remoting.Metadata.W3cXsd2001
SoapHexBinary (Framework-only)
A class that allows you to convert an array of bytes to a Base16 (hexadecimal) string and back.
using System.Runtime.Remoting.Metadata.W3cXsd2001;
var result = SoapHexBinary.Parse("A012");
// result.Value -> byte[];
System.Buffers.Text
Base64 (Core or System.Memory package)
Base64 . Span' Span .
Nuget
Multiformats.Base - Base2, 8, 16, 32, 58, 64 (+variants), MIT
BaseN . API . : .
SimpleBase - Base16, 32, 58, 85 (+variants), streaming, span, Apache-2
BaseN . .NET Core Span'. API, .
deniszykov.BaseN
, .NET Framework 4.5.
, Base64 Span<byte> to Span<byte> ( char[] <-> byte[] ). , . "" byte char , char 2 . .. , , .
/ :
byte[]
char[]
string
byte*
char*
Span<byte>
Span<char>
All this lies inside the two classes BaseNDecoder / BaseNEncoder .
The following encoding dictionaries are supported:
Base16 (hexadecimal / lower / upper)
Base32
ZBase32
Base64
Base64 Url-safe
using deniszykov.BaseN;
var bytes = Base64Convert.ToBytes("eg==");
// bytes[0] -> 122
A richer set offers the BaseNEncoding class which implements System.Text.Encoding .
using deniszykov.BaseN;
var count = BaseNEncoding.Base64Url.GetByteCount("eg=="); // -> 1
var bytes = BaseNEncoding.Base64Url.GetBytes("eg=="); // -> 122
var baseNEncoder = new BaseNEncoder(BaseNAlphabet.Base64Alphabet);
var input = "eg==";
var output = new byte[1024];
baseNEncoder.Convert(input, 0, 4, output, 0, 1, true, out var charsUsed, out var bytesUsed, out var completed);
// output[0] -> 122
Other examples of "simple" use are here . You can use your own dictionary. There will be no complex examples, those who need it can find them in tests :)
Install-Package deniszykov.BaseN