Base32, Base62, and Base64URL encoding and decoding — URL-safe, padding-free alternatives to standard Base64.
dotnet add package Philiprehberger.BaseEncodingBase32, Base62, and Base64URL encoding and decoding — URL-safe, padding-free alternatives to standard Base64.
dotnet add package Philiprehberger.BaseEncoding
using Philiprehberger.BaseEncoding;
var encoded = BaseEncoding.Base32.Encode(new byte[] { 72, 101, 108, 108, 111 });
var decoded = BaseEncoding.Base32.Decode(encoded);
using Philiprehberger.BaseEncoding;
var data = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
var base32 = BaseEncoding.Base32.Encode(data);
var original = BaseEncoding.Base32.Decode(base32);
// original == data
var base62 = BaseEncoding.Base62.Encode(data);
var restored = BaseEncoding.Base62.Decode(base62);
// restored == data
using Philiprehberger.BaseEncoding;
var tokenBytes = new byte[32];
System.Security.Cryptography.RandomNumberGenerator.Fill(tokenBytes);
var token = BaseEncoding.Base64Url.Encode(tokenBytes);
// URL-safe string with no padding, safe for query strings and headers
var raw = BaseEncoding.Base64Url.Decode(token);
using Philiprehberger.BaseEncoding;
if (BaseEncoding.Base32.TryDecode("JBSWY3DP", out var result))
{
// result contains the decoded bytes
}
else
{
// input was invalid — no exception thrown
}
using Philiprehberger.BaseEncoding;
var id = BitConverter.GetBytes(123456789L);
var shortCode = BaseEncoding.Base62.Encode(id);
// Compact alphanumeric string suitable for short URLs
var decoded = BaseEncoding.Base62.Decode(shortCode);
| Method / Type | Description |
|---|---|
BaseEncoding.Base32 | Static property returning an IBaseEncoder for RFC 4648 Base32 (no padding) |
BaseEncoding.Base62 | Static property returning an IBaseEncoder for alphanumeric [0-9A-Za-z] encoding |
BaseEncoding.Base64Url | Static property returning an IBaseEncoder for RFC 4648 section 5 Base64URL (no padding) |
IBaseEncoder.Encode(data) | Encodes a byte array into a string |
IBaseEncoder.Decode(encoded) | Decodes a string back into a byte array |
IBaseEncoder.TryDecode(encoded, out result) | Attempts to decode without throwing; returns false on invalid input |
dotnet build src/Philiprehberger.BaseEncoding.csproj --configuration Release
If you find this project useful: