Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

Base N converter (N = 10-62)

Rate me:
Please Sign up or sign in to vote.
3.37/5 (14 votes)
16 Jul 2008CPOL 82.4K   318   24   24
Can convert from Base 10 to Base 62.

Screenshot - figure1.jpg

Screenshot - figure2.jpg

Introduction

This class uses the Hex algorithm idea and expands it. Or, more like the Base64 conversion.

The Class

C#
public static class ExpandableHexConverter
{
    public enum ExpandLevel
    {
        A = 11,
        B,
        C,
        D,
        E,
        F,
        G,
        H,
        I,
        J,
        K,
        L,
        M,
        N,
        O,
        P,
        Q,
        R,
        S,
        T,
        U,
        V,
        W,
        X,
        Y,
        Z,
        UseCaseSensitive = 62
    }

    public static string ToHex(long value, ExpandLevel ExpandBy)
    {
        return loopRemainder(value, (long)ExpandBy);
    }

    public static long ToInt64(string value, ExpandLevel ExpandBy)
    {
        value = validate(value, ExpandBy);
        long returnvalue = 0;
        for (int i = 0; i < value.Length; i++)
            returnvalue += (long)Math.Pow((long)ExpandBy, 
                           (value.Length - (i + 1))) * CharToVal(value[i]);
        return returnvalue;
    }

    private static string loopRemainder(long value, long PowerOf)
    {
        long x = 0;
        long y = Math.DivRem(value, PowerOf, out x);
        if (y > 0)
            return loopRemainder(y, PowerOf) + ValToChar(x).ToString();
        else
            return ValToChar(x).ToString();
    }
    private static char ValToChar(long value)
    {
        if (value > 9)
        {
            int ascii = (65 + ((int)value - 10));
            if (ascii > 90)
                ascii += 6;
            return (char)ascii;
        }
        else
            return value.ToString()[0];
    }
    private static long CharToVal(char value)
    {
        long chrval = (int)value;
        if (chrval > 96)
            return (chrval - 71) + 10;
        else if (chrval > 64)
            return (chrval - 65) + 10;
        else
            return int.Parse(value.ToString());
    }
    private static string validate(string input, ExpandLevel ExpandBy)
    {
        string validchars = "";
        string rtnval = "";
        for (long c = 0; c < (long)ExpandBy; c++)
            validchars += ValToChar(c);
        foreach (char i in input)
            if (validchars.Contains(i.ToString()))
                rtnval += i;
        return rtnval;
    }
}

Using the code

Normal hex looks like this:

C#
string HexValue = ExpandableHexConverter.ToHex(255, 
                     ExpandableHexConverter.ExpandLevel.F);

Result:

HexValue = FF

Expanded hex could look like this:

C#
//NOTE: Z Level selected and the value to convert is larger.
string HexValue = ExpandableHexConverter.ToHex(1295, 
                        ExpandableHexConverter.ExpandLevel.Z);

Result:

Result:
HexValue = ZZ

Expanded hex could also look like this:

C#
//NOTE: UseCaseSensitive Level selected and the value 
//to convert is larger that the previus example.
string HexValue = ExpandableHexConverter.ToHex(3843, 
                      ExpandableHexConverter.ExpandLevel.UseCaseSensitive);

Result:

Result:
HexValue = zz

Some comparisons

The normal Hex Algorithm is Base 16, which is demonstrated here:

  • F = 15 (0 to 15 = 16)
  • FF = 255
  • FFF = 4095

The expanded Hex Algorithm set to Z is Base 36, which is demonstrated here:

  • Z = 35 (0 to 35 = 36)
  • ZZ = 1295
  • ZZZ = 46655 (11 times larger than normal Hex)

The expanded Hex Algorithm set to UseCaseSensitive is Base 62, which is demonstrated here:

  • z = 61 (0 to 61 = 62)
  • zz = 3843
  • zz = 238327(58 times larger than normal Hex)

History

  • 23. Nov. 2007 - The first version is posted.
  • 23. Nov. 2007 - Article renamed...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Denmark Denmark

Comments and Discussions

 
GeneralThanks Pin
westdw1-May-09 6:16
westdw1-May-09 6:16 
GeneralLimited Pin
PIEBALDconsult17-Jul-08 4:16
mvePIEBALDconsult17-Jul-08 4:16 
GeneralRe: Limited Pin
kevdelkevdel17-Jul-08 4:49
kevdelkevdel17-Jul-08 4:49 
GeneralArticle layout Pin
Jaime Olivares16-Jul-08 15:06
Jaime Olivares16-Jul-08 15:06 
GeneralRe: Article layout Pin
Paw Jershauge16-Jul-08 23:35
Paw Jershauge16-Jul-08 23:35 
AnswerRe: Article layout Pin
Jaime Olivares17-Jul-08 1:22
Jaime Olivares17-Jul-08 1:22 
QuestionIs there such thing as base 1? Pin
Rei Miyasaka24-Nov-07 21:29
Rei Miyasaka24-Nov-07 21:29 
AnswerRe: Is there such thing as base 1? Pin
Paw Jershauge25-Nov-07 0:56
Paw Jershauge25-Nov-07 0:56 
GeneralRe: Is there such thing as base 1? Pin
Rei Miyasaka25-Nov-07 12:46
Rei Miyasaka25-Nov-07 12:46 
AnswerRe: Is there such thing as base 1? Pin
colithium26-Nov-07 18:03
colithium26-Nov-07 18:03 
GeneralRe: Is there such thing as base 1? Pin
Rei Miyasaka26-Nov-07 18:53
Rei Miyasaka26-Nov-07 18:53 
GeneralRe: Is there such thing as base 1? Pin
colithium26-Nov-07 19:06
colithium26-Nov-07 19:06 
GeneralRe: Is there such thing as base 1? Pin
PIEBALDconsult17-Jul-08 4:11
mvePIEBALDconsult17-Jul-08 4:11 
GeneralRe: Is there such thing as base 1? ==&gt; Yes: Binary! Pin
scofflaw21-Jul-08 20:55
scofflaw21-Jul-08 20:55 
GeneralRe: Is there such thing as base 1? ==&gt; Yes: Binary! Pin
Darchangel22-Jul-08 3:03
Darchangel22-Jul-08 3:03 
You claimed that base 1 is binary then proceeded to show how binary is base 2. Do you not see a problem with this? The previous posts were correct: base 1 is tally marks
GeneralRe: Is there such thing as base 1? ==&gt; Yes: Binary! Pin
scofflaw22-Jul-08 3:20
scofflaw22-Jul-08 3:20 
Generalconfusing Pin
Luc Pattyn23-Nov-07 3:21
sitebuilderLuc Pattyn23-Nov-07 3:21 
GeneralRe: confusing Pin
Paw Jershauge23-Nov-07 3:30
Paw Jershauge23-Nov-07 3:30 
GeneralRe: confusing Pin
Luc Pattyn23-Nov-07 3:33
sitebuilderLuc Pattyn23-Nov-07 3:33 
GeneralRe: confusing Pin
Paw Jershauge23-Nov-07 3:38
Paw Jershauge23-Nov-07 3:38 
GeneralRe: confusing Pin
Jaime Olivares23-Nov-07 4:26
Jaime Olivares23-Nov-07 4:26 
GeneralRe: confusing Pin
Jaime Olivares23-Nov-07 4:27
Jaime Olivares23-Nov-07 4:27 
GeneralRe: confusing Pin
Paw Jershauge23-Nov-07 4:54
Paw Jershauge23-Nov-07 4:54 
GeneralRe: confusing Pin
Luc Pattyn23-Nov-07 8:10
sitebuilderLuc Pattyn23-Nov-07 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.