PandaTech.BaseConverter 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package PandaTech.BaseConverter --version 2.0.2                
NuGet\Install-Package PandaTech.BaseConverter -Version 2.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PandaTech.BaseConverter" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PandaTech.BaseConverter --version 2.0.2                
#r "nuget: PandaTech.BaseConverter, 2.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PandaTech.BaseConverter as a Cake Addin
#addin nuget:?package=PandaTech.BaseConverter&version=2.0.2

// Install PandaTech.BaseConverter as a Cake Tool
#tool nuget:?package=PandaTech.BaseConverter&version=2.0.2                

Base Converter

This project provides a class called PandaBaseConverter that can convert a number from base 10 to base 36 and vice versa. This project is available as a NuGet package as well.

Usage

Base 10 to Base 36

To convert a number from base 10 to base 36, call the Base10ToBase36 method, passing in the number as a long

long number = 12345;
string base36Number = BaseConverter.Base10ToBase36(number);

The resulting base36Number will be a string representation of the number in base 36.

Base 36 to Base 10

To convert a number from base 36 to base 10, call the Base36ToBase10 method, passing in the number as a string:

string base36Number = "2n9";
long number = BaseConverter.Base36ToBase10(base36Number);

The resulting number will be the decimal representation of the number in base 36.

Using in DTOs

public class MyDto
{
    [JsonConverter(typeof(PandaJsonBaseConverter))]
    public long Id { get; set; }
}

Using in controllers

[HttpGet("{id}")]
public async Task<ActionResult<MyDto>> Get([PandaParameterBaseConverter] long id)
{
    var myDto = await _myService.Get(id);
    return Ok(myDto);
}

In this case we will work in code with Id as long, but in json it will be as base 36 string.

Configuration

By default, the PandaBaseConverter class uses the characters "0123456789abcdefghijklmnopqrstuvwxyz" for base 36 conversion. You can configure the character set used by doing following in program.cs.

var builder = WebApplication.CreateBuilder(args);

var base36RandomCharset = "1adfgbhjklmnepqrstvwxyz23456789";

builder.ConfigureBaseConverter(base36RandomCharset);

Error Handling

If the input to either conversion method is invalid, an ArgumentException will be thrown with an appropriate error message. The message will also be printed to the console.

Prgram.cs

to use PandaParameterBaseConverter in swagger, add the following code to Program.cs


builder.Services.AddSwaggerGen(
    options => 
        options.ParameterFilter<PandaParameterBaseConverter>()
    );
    
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.3 767 6/21/2024
4.0.2 129 6/20/2024
4.0.1 130 6/19/2024
4.0.0 135 6/18/2024
3.0.6 560 5/8/2024
3.0.5 163 5/6/2024
3.0.4 139 4/25/2024
3.0.3 525 3/20/2024
3.0.1 181 3/13/2024
3.0.0 132 3/13/2024
2.0.2 499 12/2/2023
2.0.1 141 11/29/2023
1.0.18 235 11/8/2023
1.0.17 130 11/8/2023
1.0.16 269 11/7/2023
1.0.15 113 11/6/2023
1.0.14 117 11/6/2023
1.0.12 330 10/23/2023
1.0.11 220 7/19/2023
1.0.10 170 7/18/2023
1.0.9 151 7/18/2023
1.0.8 157 7/17/2023
1.0.7 194 6/7/2023
1.0.6 168 6/7/2023
1.0.5 173 6/6/2023
1.0.4 155 6/6/2023
1.0.1 173 5/30/2023
1.0.0 230 4/13/2023

New configuration option