A .NET library to encode data in Base-N. F# and C# friendly.
This library was created as an F# learning exercise. For a more performance-focused implementation, you may want to consider SimpleBase.
All encodings follow the same API pattern so you can extrapolate the following examples to any of the aforementioned encodings.
// F#
open BaseNcode.FSharp
// C#
using BaseNcode;
open System.Text
// Encode
let bytes = Encoding.UTF8.GetBytes("foobar")
let encodedString = Base32.encode true bytes
printfn encodedString // MZXW6YTBOI======
// Decode
let decodeErrorToString = function
| InvalidChar ic -> sprintf "Invalid character %c at index %i" ic.Char ic.Index
match Base32.decode encodedString with
| Ok decodedBytes -> Encoding.UTF8.GetString(decodedBytes) |> printfn // foobar
| Error errors -> Seq.map decodeErrorToString errors |> printfn
using System.Text;
// Encode
var bytes = Encoding.UTF8.GetBytes("foobar");
var encodedString = Base32.Encode(bytes);
Console.WriteLine(encodedString); // MZXW6YTBOI======
// Decode
var decodedBytes = Base32.Decode(encodedString);
Console.WriteLine(Encoding.UTF8.GetString(decodedBytes)); // foobar