Click here to Skip to main content
Licence CPOL
First Posted 23 Nov 2007
Views 34,287
Downloads 126
Bookmarked 22 times

Base N converter (N = 10-62)

By | 16 Jul 2008 | Article
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

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:

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

Result:

HexValue = FF

Expanded hex could look like this:

//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:

//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)

About the Author

Paw Jershauge

Software Developer

Denmark Denmark

Member

blog: C# And I
About.me Paw Jershauge
ListView Group Sorter
An Code Generator, for making SQL table into .Net Classses (2nd place in the Code Generation 2008 Competition)
A Class for getting the Rss feed list of a website
Seagate Date Code Calculator
DNSBL lookup Class
Base N converter (N = 10-62)
Lightweight Directory Access Protocol Uniform resource identifier (LDAPUri)
LoginHours from DirectoryEntry into boolean array

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks Pinmemberwestdw6:16 1 May '09  
GeneralLimited PinmemberPIEBALDconsult4:16 17 Jul '08  
Mine converts to/from any base from Base-2 up to however many digits you provide and requires far less code. After someone posted their Base-64 solution a week or so ago I was thinking of posting mine, but I haven't had time yet.
GeneralRe: Limited Pinmemberkevdelkevdel4:49 17 Jul '08  
GeneralArticle layout PinmemberJaime Olivares15:06 16 Jul '08  
GeneralRe: Article layout PinmemberPaw Jershauge23:35 16 Jul '08  
AnswerRe: Article layout PinmemberJaime Olivares1:22 17 Jul '08  
QuestionIs there such thing as base 1? Pinmemberreinux21:29 24 Nov '07  
AnswerRe: Is there such thing as base 1? PinmemberPawJershauge0:56 25 Nov '07  
GeneralRe: Is there such thing as base 1? Pinmemberreinux12:46 25 Nov '07  
AnswerRe: Is there such thing as base 1? PinmemberCoLithium18:03 26 Nov '07  
GeneralRe: Is there such thing as base 1? Pinmemberreinux18:53 26 Nov '07  
GeneralRe: Is there such thing as base 1? PinmemberCoLithium19:06 26 Nov '07  
GeneralRe: Is there such thing as base 1? PinmemberPIEBALDconsult4:11 17 Jul '08  
GeneralRe: Is there such thing as base 1? ==> Yes: Binary! Pinmemberscofflaw20:55 21 Jul '08  
GeneralRe: Is there such thing as base 1? ==> Yes: Binary! PinmemberDarchangel3:03 22 Jul '08  
GeneralRe: Is there such thing as base 1? ==> Yes: Binary! Pinmemberscofflaw3:20 22 Jul '08  
Generalconfusing PinmemberLuc Pattyn3:21 23 Nov '07  
GeneralRe: confusing PinmemberPawJershauge3:30 23 Nov '07  
GeneralRe: confusing PinmemberLuc Pattyn3:33 23 Nov '07  
GeneralRe: confusing PinmemberPawJershauge3:38 23 Nov '07  
GeneralRe: confusing PinmemberJaime Olivares4:26 23 Nov '07  
GeneralRe: confusing PinmemberJaime Olivares4:27 23 Nov '07  
GeneralRe: confusing PinmemberPawJershauge4:54 23 Nov '07  
GeneralRe: confusing PinmemberLuc Pattyn8:10 23 Nov '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 17 Jul 2008
Article Copyright 2007 by Paw Jershauge
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid