Skip to content

Latest commit

 

History

History
66 lines (46 loc) · 1.7 KB

README.md

File metadata and controls

66 lines (46 loc) · 1.7 KB

BaseNcode

CI

A .NET library to encode data in Base-N. F# and C# friendly.

Disclaimer

This library was created as an F# learning exercise. For a more performance-focused implementation, you may want to consider SimpleBase.

Supported Encodings

Usage

All encodings follow the same API pattern so you can extrapolate the following examples to any of the aforementioned encodings.

Import

// F#
open BaseNcode.FSharp
// C#
using BaseNcode;

F# Example

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

C# Example

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