The Pandatech.BaseConverter
library offers robust and flexible base conversion functionalities, enabling seamless
transformations between base 10 and base 36 numeral systems. Designed for simplicity and efficiency, it's an essential
tool for applications requiring numeral system conversions, particularly useful in scenarios like generating concise,
URL-friendly unique identifiers from numeric values. Available as a convenient NuGet package, it integrates effortlessly
into your .NET projects.
- Bi-directional Conversion: Supports converting numbers from base 10 to base 36 and vice versa.
- Custom Character Set Configuration: Allows customization of the character set used for base 36 encoding, enabling unique identifier generation tailored to specific requirements.
- Integration with Data Transfer Objects (DTOs): Facilitates the use of base 36 encoded strings in DTOs, enhancing API usability and readability.
- Swagger Integration: Ensures seamless integration with Swagger for API documentation and testing, with support for custom parameter converters.
- Robust Validation: Offers built-in validation for base 36 inputs, ensuring data integrity and error resilience.
- Exception Handling: Provides clear and informative error messages for invalid inputs, aiding in troubleshooting and debugging.
To include Pandatech.BaseConverter
in your project, install it as a NuGet package:
Install-Package Pandatech.BaseConverter
Converting from Base 10 to Base 36 (2 overloads for nullable and non-nullable output)
long number = 12345;
string base36Number = BaseConverter.PandaBaseConverter.Base10ToBase36(number);
// Output: base36Number = "9ix"
Converting from Base 36 to Base 10 (nullable input and output)
string base36Number = "9ix";
long? number = BaseConverter.PandaBaseConverter.Base36ToBase10(base36Number);
// Output: number = 12345
Converting from Base 36 to Base 10 (non-nullable input and output)
string base36Number = "9ix";
long number = BaseConverter.PandaBaseConverter.Base36ToBase10NotNull(base36Number, false);
// Output: number = 12345
You can customize the character set used for base 36 encoding to suit your application's needs, for not exposing actual order:
var builder = WebApplication.CreateBuilder(args);
var customCharset = "0123456789abcdefghijklmnopqrstuvwxyz";
builder.Services.ConfigureBaseConverter(customCharset);
Decorate DTO properties with [PandaPropertyBaseConverter]
to automatically handle base 36 encoding/decoding:
public class MyDto
{
[PandaPropertyBaseConverter]
public long Id { get; set; }
}
Use [PandaParameterBaseConverter]
to automatically resolve base 36 encoded parameters in controller actions:
[HttpGet("{id}")]
public async Task<ActionResult<MyDto>> Get([PandaParameterBaseConverter] long id)
{
// Your logic here
}
Validate base 36 inputs using the ValidateBase36Chars
extension method:
string base36Number = "9ix";
bool isValid = base36Number.ValidateBase36Chars();
// Output: isValid = true
builder.Services.AddSwaggerGen(
options =>
{
options.ParameterFilter<PandaParameterBaseConverterAttribute>();
options.SchemaFilter<PandaPropertyBaseConverterSwaggerFilter>();
}
);
The library employs ArgumentException
to signal invalid inputs, equipped with descriptive messages to facilitate
debugging:
try
{
var result = BaseConverter.PandaBaseConverter.Base36ToBase10("invalid-input");
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
// Handle the exception as needed
}
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.
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!