13,589,795 members
alternative version

#### Stats

61.9K views
24 bookmarked
Posted 23 Nov 2007
Licenced CPOL

# Base N converter (N = 10-62)

, 16 Jul 2008
Can convert from Base 10 to Base 62.

## 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...

## Share

 Software Developer Denmark

## You may also be interested in...

 First Prev Next
 Thanks westdw1-May-09 6:16 westdw 1-May-09 6:16
 Limited PIEBALDconsult17-Jul-08 4:16 PIEBALDconsult 17-Jul-08 4:16
 Re: Limited kevdelkevdel17-Jul-08 4:49 kevdelkevdel 17-Jul-08 4:49
 Article layout Jaime Olivares16-Jul-08 15:06 Jaime Olivares 16-Jul-08 15:06
 Re: Article layout Paw Jershauge16-Jul-08 23:35 Paw Jershauge 16-Jul-08 23:35
 Re: Article layout Jaime Olivares17-Jul-08 1:22 Jaime Olivares 17-Jul-08 1:22
 Is there such thing as base 1? reinux24-Nov-07 21:29 reinux 24-Nov-07 21:29
 Re: Is there such thing as base 1? PawJershauge25-Nov-07 0:56 PawJershauge 25-Nov-07 0:56
 Re: Is there such thing as base 1? reinux25-Nov-07 12:46 reinux 25-Nov-07 12:46
 Re: Is there such thing as base 1? CoLithium26-Nov-07 18:03 CoLithium 26-Nov-07 18:03
 Re: Is there such thing as base 1? reinux26-Nov-07 18:53 reinux 26-Nov-07 18:53
 Re: Is there such thing as base 1? CoLithium26-Nov-07 19:06 CoLithium 26-Nov-07 19:06
 Re: Is there such thing as base 1? PIEBALDconsult17-Jul-08 4:11 PIEBALDconsult 17-Jul-08 4:11
 Re: Is there such thing as base 1? ==> Yes: Binary! scofflaw21-Jul-08 20:55 scofflaw 21-Jul-08 20:55
 Re: Is there such thing as base 1? ==> Yes: Binary! Darchangel22-Jul-08 3:03 Darchangel 22-Jul-08 3:03
 Re: Is there such thing as base 1? ==> Yes: Binary! scofflaw22-Jul-08 3:20 scofflaw 22-Jul-08 3:20
 confusing Luc Pattyn23-Nov-07 3:21 Luc Pattyn 23-Nov-07 3:21
 Re: confusing PawJershauge23-Nov-07 3:30 PawJershauge 23-Nov-07 3:30
 Re: confusing Luc Pattyn23-Nov-07 3:33 Luc Pattyn 23-Nov-07 3:33
 Re: confusing PawJershauge23-Nov-07 3:38 PawJershauge 23-Nov-07 3:38
 Re: confusing Jaime Olivares23-Nov-07 4:26 Jaime Olivares 23-Nov-07 4:26
 Re: confusing Jaime Olivares23-Nov-07 4:27 Jaime Olivares 23-Nov-07 4:27
 Re: confusing PawJershauge23-Nov-07 4:54 PawJershauge 23-Nov-07 4:54
 Re: confusing Luc Pattyn23-Nov-07 8:10 Luc Pattyn 23-Nov-07 8:10
 Last Visit: 31-Dec-99 18:00     Last Update: 18-Jun-18 11:43 Refresh 1