Home .NET Base64, Base32and Base16encodings in .NET

Base64, Base32and Base16encodings in .NET

by admin

BaseN encoding

BaseN encodings are used toencode binary data into text form.Where N is the size of the text alphabet used for encoding.BaseN encodings are used to "prepare data for transmission over text protocols (HTTP, SMTP) or non-digital channels (e.g.print on paper).

The most popular version of this encoding is Base64 Comparable in terms of frequency of use can only Base16 known as hexadecimal or simply hex. There are tools for working with them in .NET since early versions, but they are not very handy.

What’s in the .NET Framework (Core)

System.Security.Cryptography

ToBase64Transform / FromBase64Transform

A pair of classes to convert to/from Base64. Using an auxiliary class CryptoStream It can be used for streaming 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 pair of functions to convert an array of bytes to and from Base64string.

using System;var bytes = System.Convert.FromBase64String("eg==")// bytes -> 122

System.BitConverter

BitConverter.ToString

Function to convert an array of bytes to a Base16 (hexadecimal) string, with each byteseparated 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 allowing to convert an array of bytes to and from Base16 (hexadecimal) string.

using System.Runtime.Remoting.Metadata.W3cXsd2001;var result = SoapHexBinary.Parse("A012");// result.Value -> byte[];

System.Buffers.Text

Base64 (Core or System.Memory package)

Handy class for Base64operation in stream mode. You can push Span ‘s with bytes and get the result in your Span bytes.

What Nuget offers

Multiformats.Base – Base2, 8, 16, 32, 58, 64 (+variants), MIT

Handy library with very many BaseN encoding variants. Simple API and there are tests. Of disadvantages : it works only with arrays.

SimpleBase – Base16, 32, 58, 85 (+variants), streaming, span, Apache-2

Library with large baseN encodings. Has .NET Core support and Span handling. Simple API, with tests.

Deniszykov.BaseN package

Of course all these classes and libraries have fatal flaw

In addition, most have dropped support for .NET Framework 4.5.

In terms of features, only the class Base64 can in Span<byte> to Span<byte> conversions (everywhere it is char[]<-> byte[] conversions). And this is a frequent case where data comes from the network. This data cannot be "interpreted" from byte in char by the fact that char occupies 2 bytes in memory. That is, to convert them, the above described libraries need to copy them to a temporary buffer twice the size of the original data.

In this package, the encoding/decoding functions accept different types of incoming data :

  • byte[]

  • char[]

  • string

  • byte*

  • char*

  • Span<byte>

  • Span<char>

All this lies inside 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 a class BaseNEncoding which implements System.Text.Encoding

using deniszykov.BaseN;var count = BaseNEncoding.Base64Url.GetByteCount("eg=="); // -> 1var bytes = BaseNEncoding.Base64Url.GetBytes("eg=="); // -> 122var 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" uses lie here You can use your dictionary. There will be no complicated examples, those who need it can find them at tests 🙂

Install package deniszykov.BaseN

You may also like