PandaTech.BaseConverter 4.0.4

dotnet add package PandaTech.BaseConverter --version 4.0.4
NuGet\Install-Package PandaTech.BaseConverter -Version 4.0.4
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="4.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PandaTech.BaseConverter --version 4.0.4
#r "nuget: PandaTech.BaseConverter, 4.0.4"
#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=4.0.4

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

Pandatech.BaseConverter

Pandatech.BaseConverter is a powerful library designed for seamless base conversion between base 10 and base 36 numeral systems. It addresses the common need in software development to obfuscate database primary keys stored as long integers by converting them into base 36 encoded strings, enhancing data confidentiality and avoiding direct exposure of entity IDs.

Features

  • Bidirectional Conversion: Convert numbers between base 10 and base 36.
  • Custom Character Set: Configure the base 36 character set for tailored encoding.
  • DTO Integration: Simplify base 36 string usage in Data Transfer Objects (DTOs).
  • Batch Conversion: Convert lists of numbers between base 10 and base 36 efficiently.
  • Swagger Support: Integrate with Swagger for API documentation and testing.
  • Validation: Ensure data integrity with robust validation for base 36 inputs.
  • Error Handling: Clear and informative exceptions for invalid inputs.

Installation

Add Pandatech.BaseConverter to your project via NuGet:

Install-Package Pandatech.BaseConverter

Basic Usage

Converting between Base 10 and Base 36

long number = 12345;
string base36Number = PandaBaseConverter.Base10ToBase36(number);
// Output: "9ix"

string base36Number = "9ix";
long? number = PandaBaseConverter.Base36ToBase10(base36Number);
// Output: 12345

long number = PandaBaseConverter.Base36ToBase10NotNull(base36Number);
// Output: 12345

var numbers = new List<long> { 12345, 67890 };
var base36Numbers = PandaBaseConverter.Base10ListToBase36List(numbers);
// Output: ["9ix", "1bqj"]

var base36Numbers = new List<string> { "9ix", "1bqj" };
var numbers = PandaBaseConverter.Base36ListToBase10List(base36Numbers);
// Output: [12345, 67890]

Advanced Usage

Customizing Base 36 Character Set

Customize the base 36 character set for your needs:

var builder = WebApplication.CreateBuilder(args);
var customCharset = "0123456789abcdefghijklmnopqrstuvwxyz";
builder.Services.ConfigureBaseConverter(customCharset);

DTO Integration

Decorate DTO properties to handle automatic base 36 encoding/decoding:

public class MyDto
{
    [PropertyBaseConverter]
    public long Id { get; set; }
}

Minimal API Parameter Binding

Use QueryBaseConverter and PathBaseConverter to automatically convert base 36 encoded parameters in minimal APIs:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var groupApp = app.MapGroup("minimal");
groupApp.MapGet("query", ([FromQuery] long id) => id)
        .QueryBaseConverter("id");

groupApp.MapGet("path/{id}", (long id) => id)
        .PathBaseConverter("id");

groupApp.MapPost("parameter", ([AsParameters] Parameter request) => request)
            .QueryBaseConverter("Prop1", "Prop2");

app.Run();

public class Parameter
{
    public long Prop1 { get; set; }
    public long Prop2 { get; set; }
}

Controller Parameter Binding

Resolve base 36 encoded parameters in controller actions using [ParameterBaseConverter]:

[ApiController]
[Route("api")]
public class ConverterController : ControllerBase
{
    [HttpGet("query")]
    public IActionResult GetFromQuery([ParameterBaseConverter, FromQuery] long id)
    {
        return Ok(id);
    }

    [HttpGet("path/{id}")]
    public IActionResult GetFromPath([ParameterBaseConverter] long id)
    {
        return Ok(id);
    }
}

Swagger Integration

Integrate with Swagger for enhanced API documentation:

builder.Services.AddSwaggerGen(
    options =>
    {
        options.AddBaseConverterFilters();
    }
);

Error Handling

Pandatech.BaseConverter provides informative exceptions for handling various errors. Here's how you can manage them:

Supported Exceptions

  • UnsupportedCharacterException: Thrown when the input contains characters not supported by the current base 36 character set.
    try
    {
        var result = PandaBaseConverter.Base36ToBase10("invalid-input");
    }
    catch (UnsupportedCharacterException ex)
    {
        Console.WriteLine(ex.FullMessage);
        // Output: "Message: Input contains unsupported characters. with Value: invalid-input"
    }
  • InputValidationException: Thrown when input validation fails, such as a negative number for base 10 to base 36 conversion.
    try
    {
        var base36Number = PandaBaseConverter.Base10ToBase36(-12345);
    }
    catch (InputValidationException ex)
    {
        Console.WriteLine(ex.FullMessage);
        // Output: "Message: Number must be non-negative. with Value: -12345"
    }
  • BaseConverterException: The base exception class for all custom exceptions in this library, providing a FullMessage property that includes both the message and the associated value.
    try
    {
        // Some operation that might fail
    }
    catch (BaseConverterException ex)
    {
        Console.WriteLine(ex.FullMessage);
    }

These exceptions ensure that your applications can gracefully handle and debug errors related to base conversions.

Contributing

Contributions are welcome! If you have suggestions for improvements or encounter any issues, please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the license terms.

Thank you for choosing Pandatech.BaseConverter for your base conversion needs!

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on PandaTech.BaseConverter:

Package Downloads
PandaTech.IEnumerableFilters

This NuGet helps with filtering tables.

Pandatech.ResponseCrafter

Handling exceptions, custom Dtos.

Pandatech.EFCoreQueryMagic

Unlock the full potential of your Entity Framework Core applications with Pandatech.EFCoreQueryMagic. This innovative package empowers developers to seamlessly create dynamic, complex queries and filters for SQL tables without diving deep into the intricacies of LINQ or manual query construction. Designed to enhance productivity and maintainability, EFCoreQueryMagic automates the translation of front-end filter requests into optimized, ready-to-execute EF Core queries. Embrace the magic of streamlined data retrieval and manipulation, and elevate your applications to new heights of efficiency and performance.

Pandatech.GridifyExtensions

Pandatech.Gridify.Extensions simplifies and extends the functionality of the Gridify NuGet package. It provides additional extension methods and functionality to streamline data filtering and pagination, making it more intuitive and powerful to use in .NET applications. Our enhancements ensure more flexibility, reduce boilerplate code, and improve overall developer productivity when working with Gridify.

PandaTech.FileExporter

Export table data into xls, xlsx, csv, pdf formats

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.4 44 6/24/2024
4.0.3 122 6/21/2024
4.0.2 54 6/20/2024
4.0.1 63 6/19/2024
4.0.0 84 6/18/2024
3.0.6 247 5/8/2024
3.0.5 113 5/6/2024
3.0.4 96 4/25/2024
3.0.3 392 3/20/2024
3.0.1 137 3/13/2024
3.0.0 93 3/13/2024
2.0.2 377 12/2/2023
2.0.1 102 11/29/2023
1.0.18 150 11/8/2023
1.0.17 88 11/8/2023
1.0.16 168 11/7/2023
1.0.15 79 11/6/2023
1.0.14 83 11/6/2023
1.0.12 222 10/23/2023
1.0.11 183 7/19/2023
1.0.10 138 7/18/2023
1.0.9 122 7/18/2023
1.0.8 127 7/17/2023
1.0.7 164 6/7/2023
1.0.6 126 6/7/2023
1.0.5 139 6/6/2023
1.0.4 123 6/6/2023
1.0.1 149 5/30/2023
1.0.0 195 4/13/2023

Default Charset Removal