BlazeOrange.ninja

About Me Disclaimer Badges Contact Me

Translating A Number to An Arbitrary Base

For the sake of ease of copying, sites like Reddit and Imagur have been shortening ids using base 62 encoding. Bitcoin addresses use base 58 (would be base 62 except 0, O, 1, and l are removed). Now you can turn a number into whartever base you like using whatever characters to represent them!

WARNING: If you want your encoded identifiers to be resistant to scraping, use a value from a Cryptographically Secure Pseudo-Random Number Generator as an input to this function. This function by itself is security by obscurity at best.

A bit of explanation (and disclaimers) for the code below:

using System;

public class Program
{
    public static void Main()
    {
        string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        decimal input = 63m;
        Console.WriteLine(BaseEncode(input, characters));
    }

    public static string BaseEncode(decimal number, string characters)
    {
        decimal highestPower = 0;
        decimal index = 0;
        decimal remainder = 0;
        try
        {
            decimal numCharacters = characters.Length;
            // Invalid value check
            if(number != Decimal.Floor(number) || number < 0)
            {
                return "";
            }
            if(number < numCharacters)
            {
                return characters[(int)number] + "";
            }
            highestPower = (decimal)Math.Pow((double)numCharacters, Math.Floor(Math.Log((double)number, (double)numCharacters)));
            index = (decimal)Math.Floor(number / highestPower);
            remainder = number - (index * highestPower);
            return characters[(int)index] + BaseEncode(remainder, characters);
        }
        catch
        {
            Console.WriteLine("Index: " + index);
            Console.WriteLine("HighestPower: " + highestPower);
            return "";
        }
    }
}

Day 16 of #100DaysToOffload


This website is designed to last.